There are two methods:
Method 1: Use the java.util.Date class to implement it, and combine it with the java.text.DateFormat class to implement time formatting. See the following code:
Copy the code code as follows:
import java.util.*;
import java.text.*;
//The following default time and date display modes are all in Chinese language.
//General language can default to Chinese, and the time and date format defaults to MEDIUM style, for example: 2008-6-16 20:54:53
//The dates and times shown below are all based on the Date class. You can also use the Calendar class to implement the class TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //Default style in the default language (Chinese) (MEDIUM style, for example: 2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //Use SHORT style to display date and time
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //Display date, week, time (accurate to seconds)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //Display date. Time (accurate to seconds)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //Display date and time (accurate to minutes)
String str7 = d7.format(now);
DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //Display date and time (accurate to minutes)
String str8 = d8.format(now);//Compared with SHORT style, this method is the best
System.out.println("Display time in Date mode: " + now);//The results displayed by this method are the same as Calendar.getInstance().getTime()
System.out.println("After formatting the time with DateFormat.getDateInstance(): " + str1);
System.out.println("After formatting the time with DateFormat.getDateTimeInstance(): " + str2);
System.out.println("After formatting the time with DateFormat.getTimeInstance(): " + str3);
System.out.println("After formatting the time with DateFormat.getInstance(): " + str4);
System.out.println("After formatting the time with DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL): " + str5);
System.out.println("After formatting the time with DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG): " + str6);
System.out.println("After formatting the time with DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT): " + str7);
System.out.println("After formatting the time with DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM): " + str8);
}
}
Running results:
Copy the code code as follows:
Display time in Date mode: Mon Jun 16 20:54:53 CST 2008
After formatting the time with DateFormat.getDateInstance(), it is: 2008-6-16
After formatting the time with DateFormat.getDateTimeInstance() it is: 2008-6-16 20:54:53
After formatting the time with DateFormat.getTimeInstance() it is: 20:54:53
After formatting the time with DateFormat.getInstance(), it is: 08-6-16 8:54 pm
After formatting the time with DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL), it is: Monday, June 16, 2008, 08:54:53 pm CST
After formatting the time with DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG), it is: 08:54:53 pm on June 16, 2008
After formatting the time with DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT): 08-6-16 8:54 pm
After formatting the time with DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM): 2008-6-16 20:54:53
Method 2: Use the java.util.Calendar class to implement it, see below:
Copy the code code as follows:
import java.util.*;
import java.text.*;
//The following uses the Calendar class to implement date and time, which is simpler than the Date class.
public class TestDate2 {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//Get the year
int month=ca.get(Calendar.MONTH);//Get the month
int day=ca.get(Calendar.DATE);//Get the day
int minute=ca.get(Calendar.MINUTE);//minute
int hour=ca.get(Calendar.HOUR);//Hour
int second=ca.get(Calendar.SECOND);//seconds
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
System.out.println("Use Calendar.getInstance().getTime() to display time: " + ca.getTime());
System.out.println("The date obtained using Calendar is: " + year + "year" + month + "month" + day + "day");
System.out.println("Use Calendar to obtain the time: " + hour + "hour" + minute + "minute" + second + "second");
System.out.println(WeekOfYear);//Shows the day of the week today is (the example I made happens to be Tuesday, so the result displays 2. If you run it on Saturday, it displays 6)
}
}
The running result is:
Copy the code code as follows:
Use Calendar.getInstance().getTime() to display time: Mon Jun 16 21:54:21 CST 2008
The date obtained using Calendar is: May 16, 2008
The time obtained using Calendar is: 9 hours, 54 minutes and 21 seconds
Summary: Generally speaking, method two is the most convenient. Method one seems too clumsy, but it depends on personal preference.
There is another way to use
Copy the code code as follows:
System.currentTimeMillis()