16 Aug 2018
java8 Date/Time Formatting/Parsing
网上搜索出的结果大部分都是通过java.text.DateFormat
和java.text.SimpleDateFormat
来format和parse java date and time。
DateFormat/SimpleDateFormat与DateTimeFormatter的比较
DateTimeFormatter
is threadsafe and immutable.
传统实现方式:
private final String dateTimeFormatPattern = "yyyy/MM/dd HH:mm:ss z";
private final Date now = new Date();
private final String dateTimeString = "2014/09/03 13:59:50 MDT";
public void demonstrateSimpleDateFormatFormatting()
{
final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);
final String nowString = format.format(now);
out.println(
"Date '" + now + "' formatted with SimpleDateFormat and '"
+ dateTimeFormatPattern + "': " + nowString);
}
public void demonstrateSimpleDateFormatParsing()
{
final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);
try
{
final Date parsedDate = format.parse(dateTimeString);
out.println("'" + dateTimeString + "' is parsed with SimpleDateFormat as " + parsedDate);
}
// DateFormat.parse(String) throws a checked exception
catch (ParseException parseException)
{
out.println(
"ERROR: Unable to parse date/time String '"
+ dateTimeString + "' with pattern '"
+ dateTimeFormatPattern + "'.");
}
}
java8 的实现方式:
date/time类不再使用java.util
包,而是java.time
包,同样的,date/time formatting/parsing类不再是java.text
包,而是来自java.time.format
包。
The java.time
package offers numerous classes for modeling dates and/or times. These include classes :
- model dates only (no time information),
- model times only (no date information),
- model date and time information,
- use timezone information,
- do not incorporate time zone information.
private final ZonedDateTime now8 = ZonedDateTime.now();
private final String dateTimeFormatPattern = "yyyy/MM/dd HH:mm:ss z";
public void demonstrateDateTimeFormatFormatting()
{
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(dateTimeFormatPattern);
final String nowString = formatter.format(now8);
out.println(
now8 + " formatted with DateTimeFormatter and '"
+ dateTimeFormatPattern + "': " + nowString);
}
public void demonstrateDateTimeFormatParsingTemporalStaticMethod()
{
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(dateTimeFormatPattern);
final ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, formatter);
out.println(
"'" + dateTimeString
+ "' is parsed with DateTimeFormatter and ZonedDateTime.parse as "
+ zonedDateTime);
}
public void demonstrateDateTimeFormatFormattingAndParsingInstant()
{
// Instant instances don't have timezone information
final Instant instant = now.toInstant();
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern(
dateTimeFormatPattern).withZone(ZoneId.systemDefault());
final String formattedInstance = formatter.format(instant);
out.println(
"Instant " + instant + " formatted with DateTimeFormatter and '"
+ dateTimeFormatPattern + "' to '" + formattedInstance + "'");
final Instant instant2 =
formatter.parse(formattedInstance, ZonedDateTime::from).toInstant();
out.println(formattedInstance + " parsed back to " + instant2);
}
Til next time,
at 10:42