Home  >  Article  >  Java  >  Use of java8 DateTimeFormatter

Use of java8 DateTimeFormatter

(*-*)浩
(*-*)浩Original
2020-01-14 15:45:594342browse

Use of java8 DateTimeFormatter

DateTimeFormatter is a new feature of java8 and is thread-safe.

The support for time zones is also relatively good.                                                                                                                                     (Recommended learning: java course )

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EE yyyy-MM-dd hh:mm:ss");
String format = dateTimeFormatter.format(datetime);
System.out.println(format);

// Locale.US 的作用是格式化时,会按照当地的习惯来格式化,如中国是 星期日,美国是Sun
DateTimeFormatter us = DateTimeFormatter.ofPattern("EE yyyy-MM-dd hh:mm:ss",Locale.US);
String us_format = us.format(datetime);
System.out.println(us_format);

DateTimeFormatter comes with many commonly used formats, such as:

//datetime,date,time
LocalDateTime now = LocalDateTime.now();
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(now));
System.out.println(DateTimeFormatter.ISO_DATE.format(now));
System.out.println(DateTimeFormatter.ISO_TIME.format(now));

//local  datetime,date,time
System.out.println(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(now));
System.out.println(DateTimeFormatter.ISO_LOCAL_DATE.format(now));
System.out.println(DateTimeFormatter.ISO_LOCAL_TIME.format(now));

//毫秒
Instant now_instance = Instant.now();
System.out.println(DateTimeFormatter.ISO_INSTANT.format(now_instance));

More Java For related technical articles, please visit the Java Tutorial column to learn!

The above is the detailed content of Use of java8 DateTimeFormatter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn