Home > Java > javaTutorial > body text

Format date and time using new DateTimeFormatter class in Java 11

PHPz
Release: 2023-07-30 13:49:34
Original
1739 people have browsed it

Use the new DateTimeFormatter class in Java 11 to format dates and times

Java 11 introduces a new date and time API that contains many new classes and methods to handle dates and times . Among these new classes, the DateTimeFormatter class is one of the key classes used for formatting and parsing dates and times. In this article, we will explain how to use the DateTimeFormatter class to format dates and times and provide some code examples to illustrate its usage.

Date and time formatting in Java is usually implemented through the SimpleDateFormat class. However, in Java 11, in order to provide better performance and richer functionality, it is recommended to use the new DateTimeFormatter class. The DateTimeFormatter class is thread-safe and provides very convenient methods for creating and using date and time format templates.

First, we need to import the java.time.format package to use the DateTimeFormatter class. The following is a sample code snippet:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatterExample {
    public static void main(String[] args) {
         // 创建一个DateTimeFormatter实例
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

        // 创建当前日期和时间的实例
        LocalDateTime now = LocalDateTime.now();

        // 格式化日期和时间
        String formattedDateTime = now.format(formatter);

        // 打印结果
        System.out.println("Formatted DateTime: " + formattedDateTime);
    }
}
Copy after login

In this example, we first create a DateTimeFormatter instance and use the ofPattern() method to specify the format template of date and time. The letters in the format template represent specific dates and times. time part. For example, "yyyy" represents the year, "MM" represents the month, "dd" represents the date, "HH" represents the hour in the 24-hour format, "mm" represents the minute, and "ss" represents the second.

Next, we create a LocalDateTime instance now, which represents the current date and time. Then, we use the format() method of DateTimeFormatter to format this date and time instance according to the format template, and save the result in a string formattedDateTime. Finally, we print out the formatted result.

Run the above code, we can get a date and time string in the format of "yyyy/MM/dd HH:mm:ss". This is the basic process of using the DateTimeFormatter class to format dates and times.

In addition to basic date and time formatting, the DateTimeFormatter class provides many other functions. For example, we can use the withZone() method to convert a date and time to a specified time zone, use the parse() method to parse a string into a date and time object, and use the formatTo() method to append the formatted date and time into an existing StringBuilder and so on.

To summarize, the DateTimeFormatter class in Java 11 provides a simple and powerful way to format and parse dates and times. Using the DateTimeFormatter class, we can not only easily create and use date and time format templates, but also get better performance and richer features. I hope this article has helped you understand and use the DateTimeFormatter class.

Reference:

  • Java SE 11 Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java /time/format/DateTimeFormatter.html

The above is the detailed content of Format date and time using new DateTimeFormatter class in Java 11. 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