Programming: Java Date, Calendar, DateFormat

xiaoxiao2021-03-05  21

Java.util.date is used to represent a point in time, and the inside is stored in a long, and the accuracy is MilliseCond. JDK 1.0, because there is no difference in the differences between the world's time, it is a problem with Date Class, if you can use Year, Month, Date to create a DATE object, or take a time point The information on the date of the year. But these materials are basically preset as GMT-based. However, considering the time zone, calendar, and various representations of the world, since JDK1.1, for time processing, it is mainly divided into Date, Calendar, DateFormat, three groups of Class, and needs to cooperate with Locale and Timezone. . Date is simply used to represent a point in time, while Calendar is used to handle different calendar problems. DateFormat handles tablets for different times. The original Date's related functions are all marked as deprecated.

For Date, New Date () is to create a DATE object that represents the current time, and the other can take the current time is system.currenttimemillis (), back to LONG. And Date is probably only the functionality of comparison.

Date is from January 1, 1970, 00:00:00 GMT. If you want to take a certain day, you must first know what calendar is calculated. Java presets is to use the Gregorian calendar, and it is also a calendar in the usual calculation. Java uses Calendar to handle calendar problems, Calendar is Abstract Class, so Java preset also sets Gregoriancalendar Class, when using Calendar.GetInstance (), preset is to return this class (there are other calendar, such as http: // Icu.sourceForge.Net/apiref/icu4j/com/ibm/icu/util/chinesecalendar.html and http://icu.sourceforge.net/apiref/icu4j/com/ibm/icu/util/hebrewcalendar.html, etc.). GetInstance () can also be used when the parameter is incorporated by Locale and Timezone, and the return of the GregorianCalendar (of course, the date algorithm is still the same) with different timezone. Basically Calendar's usage is to obtain a Calendar's object, set it to it, and then remove its corresponding year and month. Calendar does not have a form of getYear () because each different calendar has a different computing item (such as something such as the lunar calendar, there is also a timing method such as what is ugly, so only get () The function, but also to the item to be obtained, such as get (Calendar.year), Get (Calendar.day_Of_Month). (It is worth noting that the month is calculated 0, so 3 is April).

Calendar Calendar = Calendar.getInstance (); DATE D = New Date (); Calendar.SetTime (D); System.out.Println ("Year:" Calendar.get (Calendar.Year));

Another Calendar usage is to generate Date objects representing a year, usage is to get Calendar, set the year and month, and then get Date. Calendar Calendar = Calendar.GetInstance (); Calendar.Set (2005, 3, 10, 0, 0, 0); System.out.Println; Calendar.getTime ());

DateFormat is used to process time representations, this is the most complex part. DateFormat is still a Abstract Class, and we will use SimpleDateFormat. Simply, DateFormat is used to define a representation of a certain time, which can be used to take the time print, as follows usage,

DateFormat DateFormat = New SimpleDateFormat ("eeee-mmmm-dd-yyyy); DATE D = new date (); system.out.println (DateFormat.Format (D));

DateFormat and Calendar, Locale are related to DateFormat to calculate the numbers to display with Calendar, and use locale to decide to display the different language different representations, such as

Date d = new Date (); DateFormat dateformat; dateformat = DateFormat.getDateInstance (DateFormat.FULL, Locale.ENGLISH); System.out.println (dateformat.format (d)); dateformat = DateFormat.getDateInstance (DateFormat.FULL, Locale.japanese; system.out.println (DateFormat.Format (D)); DateFormat = DateFormat.getdateInstance (DateFormat.full, locale.chinese); System.Out.println (DateFormat.Format (D));

Of course, DateFormat can also turn the time of the text to the DATE object, but pay attention to this action is easy (because the time format is very complicated):

Date D; DateFormat DateFormat; DateFormat = New SimpleDateFormat ("Eeee-mmmm-DD-YYYY-HH-MM-SS"); D = DATEFORMAT.PARSE ("Tuesday - April-19-2005-18-25-17" ); System.out.println (d);

There is also a very important thing, DateFormat is not Thread Safe, so you can't share the same DateFormat in multiple Thread in the case of Multithread.

Also java.sql.date, java.sql.time, java.sql.timestamp also is related to Date, basically this is DATE, just plus DATEBASE-related features, so the usage is also the same as DATE.

转载请注明原文地址:https://www.9cbs.com/read-33083.html

New Post(0)