Date, time and other data may be needed during program design, so in this section we will introduce the Date class in the java.util package. The Date class can be used to process data related to date and time.
Objects created using the parameterless constructor of the Date class can obtain the current date and time of the local machine, for example:
DatenowTime=newDate();
Then, the date and time contained in the current nowTime object are the date and time of the local computer when the nowTime object was created. For example, assuming the current time is 20:02:32 on February 12, 2012 (CST time zone):
System.out.println(nowTime);
Then the output result at this time is:
SunFeb1220:02:32CST2012
The computer system sets the "AD" of its own time to 0:00 on January 1, 1970 (Greenwich Mean Time). You can use the Date constructor with parameters Date (long time) to create a Date object based on this time. ,For example:
Datedatel=newDate(1000),date2=newDate(-1000);
The positive number of the parameter indicates the time after AD, and the negative number indicates the time before BC. For example: 1000 means 1000 milliseconds, then the date and time contained in datel are the date and time at 1 second AD on the computer system.
If the local time zone of the running Java program is Beijing time zone (8 hours different from Greenwich Mean Time), then the above datel is 8:00:1 on January 1, 1970, and date2 is 7:59 on January 1, 1970 Minutes 59 seconds.
We can also use the static method public long currentTimeMillis() of the System class to obtain the current time of the system. If the local time zone where the Java program is running is the Beijing time zone, this time is the milliseconds that have elapsed from 8:00 on January 1, 1970 to the current time. Number, this is a big number.
The default order in which a Date object represents time is: week, month, day, hour, minute, second, year. For example: Sun Feb 12 20:02:32 CST 2012.