Java USES the String class to format the current date implementation code

  • 2020-04-01 02:59:22
  • OfStack

When output date information, it is often necessary to output date format in different formats. This example introduces the date format method in the String String class. The example outputs date format parameter values of the String class in different ways.

The idea is as follows: for example, to output the English abbreviation of month, through the format() method of String class, the first parameter is specified as location. US, the default is a number, the second parameter is % TB to represent the abbreviation of month, the third parameter is the Date() class object.

The code is as follows:


import java.util.Date;
import java.util.Locale;

public class Example1 {
    public static void main(String[] args) {
        Date today = new Date();
        //The formatted string is an abbreviation for month
        String a = String.format(Locale.US, "%tb", today);
        System.out.println(" The formatted string is an abbreviation for month : " + a);
        //The formatted string is written in English for the month
        String b = String.format(Locale.US, "%tB", today);
        System.out.println(" The formatted string is an abbreviation for month : " + b);
        //The formatted string is the week (e.g., Monday)
        String c = String.format("%ta", today);
        System.out.println(" The month formatted string is the week : " + c);
        //The formatted string is the week (e.g., Monday)
        String d = String.format("%tA", today);
        System.out.println(" The formatted string is the week : " + d);
        //The formatted string is a 4-bit year value
        String e = String.format("%tY", today);
        System.out.println(" The formatted string is 4 The year value of bits : " + e);
        //The formatted string is a 2-bit year value
        String f = String.format("%ty", today);
        System.out.println(" The formatted string is 2 The year value of bits : " + f);
        //The formatted string is a 2-bit month value
        String g = String.format("%tm", today);
        System.out.println(" The formatted string is 2 The month value of bits : " + g);
        //The formatted string is a 2-bit date value
        String h = String.format("%td", today);
        System.out.println(" The formatted string is 2 The date value of the bit : " + h);
        //The formatted string is a 1-bit date value
        String i = String.format("%te", today);
        System.out.println(" The formatted string is 1 The date value of the bit : " + i);
    }
}

The effect is as follows:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201402/2014225155215498.png" >


Related articles: