Date and time of wrapper classes
1, Data class
Java date and time using Data class. Data class java.util package.
Data class constructor:
1) Data () to initialize the object using the current time;
2) Data (long millisec) adopted January 1, 1970 the number of milliseconds from time to initialize the object.
Data class methods:
1) boolean after(Data d)、boolean before(Data d)
Data object parameter specifies the date and sooner or later than, returns a boolean value.
2) boolean equals(Object data)
Analyzing Data object and date parameters are equal.
3) int compareTo(Data d)
Comparison of Data Objects and date parameters, int value returns:
Is equal to 0 is returned;
Data object parameter after a date parameter, returns a positive number;
Otherwise it returns negative
4) long getTime( )
Returns the number of time in milliseconds from 00:00:00 GMT January 1, 1970
5) void setTime(long time)
Set the date and time with the number of time in milliseconds from 00:00:00 GMT January 1, 1970
6) String toString()
Date object converted to the form of String: dow mon dd hh: mm: ss zzz yyyy, wherein identifying dow week
2, Date format:
A, SimpleDateFormat class
SimpleDateFormat class is a class formatted date. SimpleDataFormat class is a locale-sensitive manner to formatting and parsing date. Example:
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
ft.format(dNow)
Second, the use printf format Data object output format:
printf method can easily format the time and date. Using two-letter format, it starts to represent the% t and ends with the letter format.
1) %tc
All information including the date and time Saturday Shiyue 27 14:21:20 CST 2007
2) %tF
"Year - Month - Day" format 2007-10-27
3) %tD "
Month / day / year "format 10/27/07
4) %tr
"HH: MM: SS PM" format (12 hour) 02:25:51 pm
5) %tT
"HH: MM: SS" format (24-hour format) 14:28:16
6) %tR
"HH: MM" format (24-hour format) 14:28
Third, the time resolution time string objects:
SimpleDateFormat class has some additional methods, in particular the parse (), which attempts to follow the given object SimpleDateFormat storage format to parse the string. Example:
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
String input = "1818-11-11";
Date t;
t = ft.parse(input);
3, the process of hibernation
You can use the following ways to enter a standstill (blocks the current thread), so that the CPU usage. Example:
Thread.sleep (1000 * 3); // Sleep 3 seconds
4, Calender class
How to set up and access specific parts of it date data, such as hours, days, or minutes? How in these parts date plus or minus value? The answer is to use Calendar category.
Calendar functions like a lot of powerful than the Date class, but also in the implementation of the Date class than more complicated.
Calendar class is an abstract class that implements object-specific subclass in actual use, the creation of objects is transparent to the programmer only needs to be created to use the getInstance method.
1) Create a Calendar class object. Because the Calendar class is an abstract class, creates an object from different ways:
Calendar c = Calendar.getInstance (); // create a Calendar object, time is the current time
Time 2) modify the calendar object
A mode: public final void set (int year, int month, int date)
Example:
c.set(2009, 6 - 1, 12);
Second way: Specify the date in a field, modify
Calendar.YEAR Year
Calendar.MONTH month
Calendar.DATE date
Calendar.DAY_OF_MONTH date, and fields exactly the same meanings as described above
Calendar.HOUR 12 hour clock
Calendar.HOUR_OF_DAY 24 hour clock
Calendar.MINUTE minutes
Calendar.SECOND 秒
Calendar.DAY_OF_WEEK week
Example:
c.set(Calendar.DATE,10);
c.set(Calendar.YEAR,2008);
3) Calendar object to obtain a field of concrete
Example:
// Get Year
int year = c1.get(Calendar.YEAR);
// get minutes
int minute = c1.get(Calendar.MINUTE);
// get seconds
int second = c1.get(Calendar.SECOND);
// get the week (note (this class is different and Date): 1 for Sunday, 2 for week 1, 3 representatives on Tuesday, and so on)
int day = c1.get(Calendar.DAY_OF_WEEK);
DETAILED a subtraction operation 4) Calendar object
Example:
c.add (Calendar.DATE, 10); // 10 days time
c.add (Calendar.DATE, -10); // 10 days before the time
5、 GregorianCalendar
GregorianCalendar class implements the Gregorian calendar. See the class definition.