在 Java 中将字符串转换为日期
原文:https://www.studytonight.com/java-examples/convert-string-to-date-in-java
字符串可以存储日期,但不能用于执行常见的日期操作,例如从日期中提取一年中的月份。在本教程中,我们将学习如何在 Java
中将字符串转换为日期,以便对其应用与日期相关的操作。
将字符串转换为本地日期和本地日期时间
String
对象可以使用parse()
方法轻松转换为LocalDate
和LocalDateTime
类的对象。确保字符串包含有效的日期和时间格式,否则我们可能会得到一个DateTimeParseException
。
public static void main(String[] args)
{
//LocalDate
String strDate = "2021-07-23";
LocalDate date = LocalDate.parse(strDate);
System.out.println(date);
//LocalDateTime
String strDateTime = "2021-07-23T10:30:59";
LocalDateTime dateTime = LocalDateTime.parse(strDateTime);
System.out.println(dateTime);
}
2021-07-23 2021-07-23t 10:30:59
这种方法的一个缺点是,如果字符串包含一些不被parse()
方法识别的其他日期格式,那么我们将得到一个DateTimeParseException
。下面的代码演示了这个场景。
public static void main(String[] args)
{
String strDate = "2021-July-23";
LocalDate date = LocalDate.parse(strDate);
System.out.println(date);
}
线程“main”Java . time . format . datetime arseeexception 中的异常:无法在索引 5 处解析文本“2021-7-23”
使用自定义格式化器将String
转换为Date
有时字符串可能包含其他格式的日期,parse()
方法可能无法识别这种格式。在这种情况下,我们可以使用自定义格式化器。Java 有一个DateTimeFormatter
类和一个SimpleDateFormat
类,可以用来为我们的字符串指定一种格式。然后,我们可以使用这些类的parse()
方法来解析字符串,并将其转换为适当的日期。
下面的列表显示了格式化字符串时最常用的日期和时间模式。
- 像 2021 年,或者 21 年
- 一年中的 m 月,比如 7 月,或者 07 年
- 一个月中的第几天
- 像星期五或 Fri 这样的电子日名称
- 上午/下午标记
- 小时(0-23)
- 上午和下午的小时数(0-12)
- 米-分钟(0-59)
- 秒(0-59)
下面显示了几个 DateTimeFormatter 类的例子。
格式日期-年-月-日
让我们尝试将一个简单的日期格式从字符串转换为本地日期。我们将使用 yyyy 作为年,单个 M 作为月,单个 d 作为月日。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class demo
{
public static void main(String[] args)
{
String strDate1 = "2021-07-23";
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-M-d");
LocalDate d1 = LocalDate.parse(strDate1, f1);
System.out.println(d1);
}
}
2021-07-23
格式日期-日名、月名、年
现在,日期中还包含了日期名称。为了解析这个字符串,我们将使用 EEEE 作为全天名称。日期用 d ,月份用 M ,年份用yyy。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class demo
{
public static void main(String[] args)
{
String strDate1 = "Friday, 23/07/2021";
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("EEEE, d/M/yyyy");
LocalDate d1 = LocalDate.parse(strDate1, f1);
System.out.println(d1);
}
}
2021-07-23
格式日期-三个字母的日名、日月名、年
这一次,我们在字符串中有一个三个字母的日名称和完整的月名称。我们将使用 EEE 作为日名, MMMM 作为月名。字母 d 表示日期,yyy表示年份。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class demo
{
public static void main(String[] args)
{
String strDate1 = "Fri, 23 July, 2021";
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("EEE, d MMMM, yyyy");
LocalDate d1 = LocalDate.parse(strDate1, f1);
System.out.println(d1);
}
}
2021-07-23
格式的日期和时间-日-月-年时:分:秒上午/下午
我们也把时间包含在字符串中。对于时间的小时部分,我们将使用 HH 。几分钟内,我们将使用 mm ,几秒钟内,我们将使用 ss 。我们也将使用字母 a 表示上午和下午。
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class demo
{
public static void main(String[] args)
{
String strDate1 = "23-07-2021 10:30:59 AM";
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("d-M-yyyy HH:mm:ss a");
LocalDate d1 = LocalDate.parse(strDate1, f1);
System.out.println(d1);
}
}
2021-07-23
简单日期格式类的工作如下所示。我们需要将代码放在一个 try-catch 块中,以避免 ParseException。
格式日期-年-月-日
我们用 yyyy 表示年份, M 表示月份,用单个 d 表示月份的日期。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo
{
public static void main(String[] args)
{
String strDate1 = "2021-07-23";
SimpleDateFormat f1 = new SimpleDateFormat("yyyy-M-d");
try
{
Date d1 = f1.parse(strDate1);
System.out.println(d1);
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
寒冷的 7 月 23 日 00:00:00 是 2021
格式日期-全天名称,年月日
我们将使用 EEEE 作为完整的日名称来解析带有日名称的字符串。日期用 d ,月份用 M ,年份用yyy。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo
{
public static void main(String[] args)
{
String strDate1 = "Friday, 23/07/2021";
SimpleDateFormat f1 = new SimpleDateFormat("EEEE, d/M/yyyy");
try
{
Date d1 = f1.parse(strDate1);
System.out.println(d1);
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
fri jul 23:00:00 是 2021
格式日期-三个字母的日名、日月名、年
三个字母的日名可以用 EEE 来表示。我们将使用 MMM 作为完整的月份名称。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo
{
public static void main(String[] args)
{
String strDate1 = "Fri, 23 July, 2021";
SimpleDateFormat f1 = new SimpleDateFormat("EEE, d MMM, yyyy");
try
{
Date d1 = f1.parse(strDate1);
System.out.println(d1);
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
fri jul 23:00:00 是 2021
格式的日期和时间-日-月-年时:分:秒上午/下午
对于时间的小时部分,我们将使用 HH 。使用 mm 分钟,我们将使用 ss 秒钟。我们也将使用字母 a 表示上午和下午。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo
{
public static void main(String[] args)
{
String strDate1 = "23-07-2021 10:30:59 AM";
SimpleDateFormat f1 = new SimpleDateFormat("d-M-yyyy HH:mm:ss a");
try
{
Date d1 = f1.parse(strDate1);
System.out.println(d1);
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
fri jul 23:00:00 是 2021
使用时区
日期字符串中的字符 Z 用于表示时区。默认情况下,SimpleDateFormat 类将显示我们为 JVM 设置的时区。我们可以使用 SimpleDateFormat 类的 setTimeZone()方法来查看不同的时间。然后,这个时间被转换为 JVM 时区。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class demo
{
public static void main(String[] args)
{
String strDate1 = "23-07-2021 10:30:59Z";
SimpleDateFormat f1 = new SimpleDateFormat("d-M-yyyy HH:mm:ss");
SimpleDateFormat f2 = new SimpleDateFormat("d-M-yyyy HH:mm:ss");
f2.setTimeZone(TimeZone.getTimeZone("GMT"));
try
{
Date d1 = f1.parse(strDate1);
Date d2 = f2.parse(strDate1);
System.out.println(d1);
System.out.println(d2);
}
catch(ParseException e)
{
System.out.println(e);
}
}
}
Fri Jul 23 10:30:59 是 2021 Fri Jul 23 16:00:59 是 2021
我们也可以使用 ZonedDateTime 来处理时区。
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class demo
{
public static void main(String[] args)
{
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
ZonedDateTime z1 = ZonedDateTime.parse("2021-07-23 10:30:59 Europe/London", f);
ZonedDateTime z2 = ZonedDateTime.parse("2021-07-23 10:30:59 Europe/Madrid", f);
System.out.println(z1);
System.out.println(z2);
}
}
2021-07-23t 10:30:59+01:00[欧洲/伦敦] 2021-07-23t 10:30:59+02:00[欧洲/马德里]
使用 Apache 公共语言 3
Apache Commons 是一个著名的库,它提供了一个 DateUtils 类来处理日期。我们可以通过使用 parseDate()方法并传递一个日期格式数组来将字符串转换为日期。
import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
public class StringToDate
{
public static void main(String[] args)
{
try
{
String dateInString = "2021-07-23";
Date date = DateUtils.parseDate(dateInString, new String[] {"yyyy-MM-dd"});
System.out.println(date);
}
catch (ParseException e)
{
System.out.print(e);
}
}
}
寒冷的 7 月 23 日 00:00:00 是 2021
摘要
本教程解释了如何将字符串转换为日期。如果我们只想存储日期并打印它们,字符串就可以了,但是它们不允许我们执行日期值的其他操作。我们可以使用 LocalDate 和 LocalDateTime 类的 parse()方法将包含日期的字符串转换为标准格式。如果字符串是其他日期格式,那么我们可以使用 DateTimeFormatter 类或 SimpleDateFormat 类定义一个自定义格式化器。