To implement the formatting of dates, you need to use the class: java.text.DateFormat
DateFormat does not have a constructor that can be used directly. Generally, the subclass of DateFormate - java.text.SimpleDateFormat is used to complete the construction.
public SimpleDateFormat(String pattern)
Test code
The code copy is as follows:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatTest
{
public static void main(String[] args)//Note: String should not be written as Strings
{
DateFormat df = new SimpleDateFormat("yyyy year mm month dd day hh:mm:ss");
Date currentTime = new Date();
String currentTimedf = df.format(currentTime);
System.out.println("current time is: "+currentTimedf);
}
}
SimpleDateFormat has the following features
1. Accept the corresponding format string and format the various parts in the Date. Among them, yyyy represents year, MM represents month, DD represents day, hh represents hour, mm represents minute, and ss represents second
2. In the format string, except for the part that has representative meaning, other parts appear as they are.