Home > Java > javaTutorial > Use of java8 DateTimeFormatter

Use of java8 DateTimeFormatter

(*-*)浩
Release: 2020-01-14 15:45:59
Original
4394 people have browsed it

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);
Copy after login

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));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template