Java date time
The java.util package provides the Date class to encapsulate the current date and time. The Date class provides two constructors to instantiate Date objects.
The first constructor initializes the object using the current date and time.
Date( )
The second constructor receives a parameter, which is the number of milliseconds since January 1, 1970.
Date(long millisec)
After the Date object is created, you can call the following method.
Serial number | Method and description |
---|---|
1 | boolean after(Date date) If the Date object that calls this method is after the specified date, it returns true, otherwise it returns false. |
2 | boolean before(Date date) If the Date object calling this method returns true before the specified date, otherwise it returns false. |
3 | Object clone( ) Returns a copy of this object. |
4 | int compareTo(Date date) Compares the Date object and the specified date when this method is called. When the two are equal, 0 is returned. If the calling object is before the specified date, a negative number is returned. The calling object returns a positive number after the specified date. |
5 | int compareTo(Object obj) If obj is of Date type, the operation is equivalent to compareTo(Date). Otherwise it throws ClassCastException. |
6 | boolean equals(Object date) Returns true when the Date object calling this method is equal to the specified date, otherwise it returns false. |
7 | long getTime( ) Returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT. |
8 | int hashCode( ) Returns the hash code value of this object. |
9 | void setTime(long time) Set the time and date in milliseconds since January 1, 1970 00:00:00 GMT. |
10 | String toString( ) Converts a Date object to a String representation and returns the string. |
Getting the current date and time
Getting the current date and time in Java is very simple. Use the toString() method of the Date object to print the current date and time, as shown below:
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date 对象 Date date = new Date(); // 使用 toString() 函数显示日期时间 System.out.println(date.toString()); } }
The above example compilation and running results are as follows:
Mon May 04 09:51:52 CDT 2013
Date comparison
Java uses the following three methods to compare two dates :
Use the getTime() method to get two dates (the number of milliseconds since January 1, 1970), and then compare the two values.
Use the methods before(), after() and equals(). For example, if the 12th of a month is earlier than the 18th, then new Date(99, 2, 12).before(new Date (99, 2, 18)) returns true.
Use the compareTo() method, which is defined by the Comparable interface, and the Date class implements this interface.
Formatting dates using SimpleDateFormat
SimpleDateFormat is a class that formats and parses dates in a locale-sensitive manner. SimpleDateFormat allows you to choose any user-defined date and time format to run on. For example:
import java.util.*; import java.text.*; public class DateDemo { public static void main(String args[]) { Date dNow = new Date( ); SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); System.out.println("Current Date: " + ft.format(dNow)); } }
The compilation and running results of the above example are as follows:
Current Date: Sun 2004.07.18 at 04:14:09 PM PDT
Simple DateFormat formatting encoding
The time pattern string is used to specify the time format. In this mode, all ASCII letters are reserved as pattern letters, defined as follows:
Letters | Description | Example |
---|---|---|
G | Epoch mark | AD |
Four-digit year | 2001 | |
Month | July or 07 | |
The date of the month | 10 | |
A.M./P.M. (1~12) format hour | 12 | |
Hour of the day (0~23) | twenty two | |
Number of minutes | 30 | |
Seconds | 55 | |
S | Number of milliseconds | 234 |
E | Day of the week | Tuesday |
D | Days of the year | 360 |
F | Day of the week | 2 (second Wed. in July) |
Week of the year | 40 | |
Week of the month | 1 | |
A.M./P.M. Mark | PM | |
Hour of the day (1~24) | twenty four | |
K | A.M./P.M. (0~11) format hour | 10 |
z | Time zone | Eastern Standard Time |
Text delimiter | Delimiter | |
apostrophe | ` |
Characters | Description | example |
c | Complete date and time | Mon May 04 09:51:52 CDT 2009 |
F | ISO 8601 format date | 2004-02-09 |
U.S. format date (month/day/year) | 02/09/2004 | |
24 hours time | 18:05:19 | |
12 hours | 06:05:19 pm | ## R |
18:05 | Y | |
2004 | y | |
04 | ||
C | The first 2 digits of the year (including leading 0) | 20 |
B | Full name of month | February |
b | Month abbreviation | Feb |
2-digit month (including leading 0) | 02 | |
2-digit date (including leading 0) | 03 | |
2-digit date (excluding leading 0) | 9 | |
Full name of the week | Monday | |
Abbreviation of week | Mon | |
j | 3-digit year (including leading 0) | 069 |
H | 2-digit hour (including leading 0), 00 to 23 | 18 |
k | 2-digit hour (excluding leading 0), 0 to 23 | 18 |
I | 2-digit hour (including leading 0), 01 to 12 | 06 |
l | 2-digit hour (excluding leading 0), 1 to 12 | 6 |
M | 2-digit minute (including leading 0) | 05 |
S | 2-digit seconds (including leading 0) | 19 |
L | 3-digit milliseconds (including leading 0) | 047 |
N | 9-digit nanosecond (including leading 0) | 047000000 |
Uppercase afternoon sign | PM | |
Lower case uppercase afternoon sign | pm | |
RFC 822 digital offset from GMT | -0800 | |
Time zone | PST | |
Number of seconds since 1970-01-01 00:00:00 GMT | 1078884319 | |
Since 1970-01-01 00:00:00 GMT's subtlety | 1078884319047 |
Constant | Description |
---|---|
Calendar.YEAR | Year |
Calendar.MONTH | month |
Calendar.DATE | 日 |
Calendar.DAY_OF_MONTH | Date has the same meaning as the above field |
Calendar.HOUR | Hour in 12-hour format |
Calendar.HOUR_OF_DAY | Hour in 24-hour format |
Calendar.MINUTE | Minute |
Calendar. SECOND | Seconds |
Day of the week |
Serial number | Constructor and description |
GregorianCalendar()
Constructs a default GregorianCalendar using the current time in the default time zone with the default locale. | |
GregorianCalendar(int year, int month, int date)
Constructs a GregorianCalendar with the given date settings in the default time zone with the default locale | |
GregorianCalendar(int year, int month, int date, int hour, int minute)
Constructs a GregorianCalendar with the given date and time settings for the default time zone with the default locale. | |
GregorianCalendar(int year, int month, int date, int hour, int minute, int second)
Constructs a GregorianCalendar with the given date and time settings for the default time zone with the default locale. | |
GregorianCalendar(Locale aLocale)
Constructs a GregorianCalendar based on the current time in the default time zone with the given locale. | |
GregorianCalendar(TimeZone zone)
Constructs a GregorianCalendar based on the current time in the given time zone with the default locale. | |
GregorianCalendar(TimeZone zone, Locale aLocale)
Constructs a GregorianCalendar based on the current time in the given time zone with the given locale. |
Serial number | Method and instructions |
void add(int field, int amount)
Adds the specified (signed) amount of time to the given calendar field according to calendar rules. | |
protected void computeFields()
Convert UTC millisecond value to time domain value | |
protected void computeTime()
Override Calendar and convert the time domain value to UTC millisecond value | |
boolean equals(Object obj)
Compares this GregorianCalendar to the specified Object. | |
int get(int field)
Get the time value of the specified field | |
int getActualMaximum(int field)
Returns the current date, the maximum value of the given field | |
int getActualMinimum(int field)
Returns the current date, the minimum value of the given field | |
int getGreatestMinimum(int field)
Returns the highest minimum value for the given calendar field in this GregorianCalendar instance. | |
Date getGregorianChange()
Get the Gregorian calendar change date. | |
int getLeastMaximum(int field)
Returns the lowest maximum value for the given calendar field of this GregorianCalendar instance | |
int getMaximum(int field) Returns the maximum value of the given calendar field for this GregorianCalendar instance. | |
12 | Date getTime() Get the current time of the calendar. |
13 | long getTimeInMillis() Get the current time of the calendar represented by a long integer |
14 | TimeZone getTimeZone() Get the time zone. |
15 | int getMinimum(int field) Returns the minimum value of the given field. |
& |