Home  >  Article  >  Java  >  Format date and time using java's String.format() function

Format date and time using java's String.format() function

WBOY
WBOYOriginal
2023-07-25 20:49:582050browse

Use Java's String.format() function to format date and time

Introduction:
In Java, we often need to format date and time to meet specific style requirements. Java provides multiple ways to format date and time, one of the commonly used ways is to use the String.format() function. This article will introduce how to use the String.format() function to format date and time and provide code examples.

Code Example:
The following is a simple example that demonstrates how to use the String.format() function to format the current date and time.

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

public class DateTimeFormatterDemo {
    public static void main(String[] args) {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();

        // 使用默认格式(ISO_LOCAL_DATE_TIME)
        String defaultFormat = String.format("默认格式:%s", now);
        System.out.println(defaultFormat);

        // 自定义格式
        String customFormat = String.format("自定义格式:%s", now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println(customFormat);
    }
}

Analysis:
In the above code, first we use LocalDateTime.now() to get the current date and time. Then, we use the String.format() function to format the date time. In the format string, we use the placeholder %s to indicate the part that needs to be replaced. For the default format, the ISO_LOCAL_DATE_TIME format is used by default, which is yyyy-MM-ddTHH:mm:ss. For custom formats, we use the DateTimeFormatter.ofPattern() method to specify a specific format. In the example, the custom format we use is yyyy-MM-dd HH:mm:ss. Finally, we print out the formatted date and time.

Summary:
Using Java's String.format() function can easily format date and time. We can use the default format or customize a specific date and time format. By mastering the use of the String.format() function, we can better meet business needs and display date and time formats that meet the requirements.

Note:

  • In the custom format, different format characters represent different date and time parts, such as %H represents the hour in the 24-hour format , %M represents minutes. For specific format characters, please refer to Java official documentation or related materials.
  • When using the String.format() function to format date and time, you need to convert the date and time object into a string first, and then perform the formatting operation.

The above is the detailed content of Format date and time using java's String.format() function. 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