Reprint please indicate the source http://blog.csdn.net/harryweasley/article/details/41977633, thank you
This article mainly introduces the four classes of Date,dateformat,simpledateformat,calendar
Date class:
Under the Java.util package
The class Date represents a specific instantaneous, accurate to milliseconds.
Starting with JDK 1.1, you should use the Calendar class to convert between date and Time fields, using the DateFormat class to format and parse date strings. The appropriate method in Date has been deprecated.
So date is mainly used to generate time,
Date has two methods of construction
Date ()
Allocates Date
an object and initializes the object to indicate the time (in milliseconds) to allocate it.
Date (long date)
Allocates an Date
object and initializes the object to represent the specified number of milliseconds since the standard base time, known as the epoch, or January 1, 1970 00:00:00 GMT.
Date date = new Date ();D ate date2=new date (0); SYSTEM.OUT.PRINTLN (date); System.out.println (DATE2);
The output is:
Tue Dec 19:40:32 CST 2014Thu Jan 08:00:00 CST 1970
You can see the difference between the two constructors, here I personally think that the second constructor, the usefulness is too little, because it is not realistic to know the number of milliseconds of a time relative to the "calendar element".
Date Common methods:
Boolean after (date time) tests whether this date is after the specified date
Boolean before (date when) tests whether this date is before the specified date
int CompareTo (date anotherdate) compares the order of two dates
Long GetTime () returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT
void SetTime (long time) to set this date object to represent the point in the January 1, 1970 00:00:00 GMT
The self-feeling of these methods is meaningless, so it is not introduced.
DateFormat class:
Under the Java.text package
DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times in a language-independent manner.
It is an abstract class, so you cannot construct a method to instantiate it, and you can instantiate it with two static functions, Getdateinstance () and Getdatetimeinstance () . The difference between the two is that a return is a date, and a return is a date + time.
Also, getdateinstance (int style, Locale alocale)
About the style value:
Full: Longest length For example: Wednesday, January 9, 2013
Long: Longer length example: January 9, 2013
MEDIUM: Longer than short length such as: Jan 9,2013
Short: Completely digital, for example: 13/1/9
Having said so much, take a quick look at the example:
DateFormat df = dateformat.getdateinstance ();//using getdatetimeinstance to generate the format instance, you can display both date and time dateformat DF2 = Dateformat.getdatetimeinstance (); String s = df.format (date); String s2 = df2.format (date); System.out.println (s); SYSTEM.OUT.PRINTLN (S2);
The output result is
2014-12-162014-12-16 20:01:05
Can see a date, there is time, a date only.
DateFormat df3 = dateformat.getdateinstance (Dateformat.full,locale.china); String china_time = Df3.format (New Date ());D Ateformat df4 = dateformat.getdateinstance (Dateformat.long,locale.china); String g_time = Df4.format (New Date ()); SYSTEM.OUT.PRINTLN ("Full value:" + china_time); SYSTEM.OUT.PRINTLN ("Long value:" + g_time);
The output values are:
Full value: December 16, 2014 Tuesday Long value: December 16, 2014
There's no explanation here.
SimpleDateFormat class:
Under the Java.text package
It is a direct subclass of the DateFormat class, inheriting the DateFormat class. I understand the SimpleDateFormat class, it is more grounded than the Datef class, and you can give him a formal date and make changes.
The main function of the SimpleDateFormat class is the conversion of the format between completion dates, and the following steps need to be taken during the conversion process:
1. Specify a template and, based on this template, take out the first all time numbers.
2. All time numbers will be saved using the date class.
3. Re-format all the time numbers.
The template is the following table, note case sensitive
Date |
Template |
Describe |
Years |
Y |
Represents year: YYYY |
Month |
M |
Represents month: MM |
Day |
D |
Mean Day: DD |
When |
HH |
When represented: HH |
Score of |
Mm |
Indication points: mm |
Seconds |
Ss |
Represents seconds: ss |
Milliseconds |
S |
MS: SSS |
Date Date2 = new Date (); SimpleDateFormat SPF = new SimpleDateFormat ("YYYY year-MM month-DD day: hh-mm min-ss sec"); System.out.println (Spf.format (date2));
The output is:
2014-December-16th: 20 o'clock-16 minutes-49 seconds
Here are two ways to make sure that one is Spf.format (date), one is Spf.parse (string), and the other is to change the date into a new format, extract the date from the given string, and extract the date type.
At the same time, when the parse method is called, an exception occurs,
try {
} catch (ParseException e) {
E.printstacktrace ();
}
This should not be difficult to understand, it is possible that the string cannot be fetched out of date, it catches the exception.
Calendar class:
Under the Java.util package
The Calendar class is an abstract class,
It provides methods for converting between "specific moments" and a set of calendar fields such as "year", "MONTH", "Day_of_month", "HOUR", and provides methods for manipulating calendar fields, such as getting the date of the next week.
There are two ways to instantiate a calendar, the first is calendar nowtime = new GregorianCalendar (), and the second is the calendar calendar=calendar.getinstance ();
See Example:
Calendar nowtime = new GregorianCalendar ();//Gets the default date, time, and time zone under the current system platform. int todayyear = Nowtime.get (calendar.year);//Gets the "year" of the default date under the current system platform. int todaymonth = Nowtime.get (calendar.month) + 1;//Gets the "month" of the default date under the current system platform. int todayday = Nowtime.get (calendar.day_of_month); Gets the "day" of the default date under the current system platform. Outputs the time under the current system platform. System.out.println ("Now the date is:" + Todayyear + "Year" + Todaymonth + "Month" + Todayday + "Day"); Nowtime.set (2013, 2-1, 9); System.out.println (Nowtime.gettime ()); Calendar calendar3 = Calendar.getinstance (); Calendar3.add (calendar.date); int year1 = Calendar3.get (calendar.year) ;//month int month1 = Calendar3.get (calendar.month) + 1;//date int date1 = Calendar3.get (calendar.date); System.out.println (year1 + "year" + Month1 + "Month" + date1 + "Day");
The output is:
The present date is: December 16, 2014 Sat 20:29:30 CST 20,132,014 December 26
Call... Finally finished ....
The methods in Java about date classes