When outputting date information, it is often necessary to output date formats in different formats. This example introduces the date formatting method in the String string class. The example uses different methods to output the date format parameter values of the String class. Combining these values can be achieved A date string in special format.
The idea is as follows: For example, to output the English abbreviation of the month, use the format() method of the String class, the first parameter specifies the locale as Locale.US, which defaults to a number, and the second parameter is %tb to represent the abbreviation of the month, and the third The parameters are Date() class objects.
The code is as follows:
The code copy 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 the English abbreviation of the month
String a = String.format(Locale.US, "%tb", today);
System.out.println("The formatted string is the English abbreviation of the month: " + a);
// The formatted string is written in full English for the month
String b = String.format(Locale.US, "%tB", today);
System.out.println("The formatted string is the English abbreviation of the month: " + b);
// The formatted string is the week (such as Monday)
String c = String.format("%ta", today);
System.out.println("The string formatted by the month is week: " + c);
// The formatted string is the week (such as Monday)
String d = String.format("%tA", today);
System.out.println("Formatted string is week: " + d);
// The formatted string is a 4-bit year value
String e = String.format("%tY", today);
System.out.println("The formatted string is a 4-bit year value: " + e);
// The formatted string is a 2-bit year value
String f = String.format("%ty", today);
System.out.println("The formatted string is a 2-bit year value: " + f);
// The formatted string is a 2-bit month value
String g = String.format("%tm", today);
System.out.println("The formatted string is a 2-bit month value: " + g);
// The formatted string is a 2-bit date value
String h = String.format("%td", today);
System.out.println("The formatted string is a 2-bit date value: " + h);
// The formatted string is a 1-bit date value
String i = String.format("%te", today);
System.out.println("The formatted string is 1-bit date value: " + i);
}
}
The effect is shown in the picture: