Date and time class development experience

Source: Internet
Author: User
Tags date1 dateformat locale

Date and Time class development experience 1 Revision History

Version Change Time Author Description Effort Hours
1.0 2006-4-28 Levinsoft methods, resources, etc. 1
1.1 2007-8-28 Levinsoft Added: A comparison of SimpleDateFormat and Dateutis. Conversion between string and date. Add: Frequently Asked questions 1
2 IntroductionThis article is about the date and time of the relevant content. The main contents include: 1. The format of the time standard 2. Conversion between date and time and string 3. Usages of commonly used classes 4. How to call and display on a JSP page. 5. An explanation of the Apache Common Lang component 3 SimpleDateFormatIn the JDK SimpleDateFormatis defined in detail, and the time class for common Lang is also packaged and extended based on this class. It is the concrete implementation subclass of DateFormat. 1. Calendar cal = Calendar.getinstance (); SimpleDateFormat formatter = new SimpleDateFormat ("SSYYYYHHMMDDMM");   String Code=formatter.format (Cal.gettime ()); 2.     This is the way to use the new SimpleDateFormat (Amask). Parse (strdate) Wf97 program. SimpleDateFormat SimpleDateFormat = NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try{Date date1 = SimpleDateFormat. Parse (Dateutil.getdatetime ("Yyyy-mm-dd HH:mm:ss", NewDate ())); System.                       Out. println ("SimpleDateFormat is:" + date1); System. Out. println (' is: ' + dateutil.convertstringtodate (dateutil.getdatetime ("Yyyy-mm-dd HH:mm:ss") NewDate ())); } Catch(ParseException PE)        {Pe.printstacktrace (); } 3. Equivalence of two modes (1) SimpleDateFormat SimpleDateFormat = NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Datzxds.setvalidtime (Simpledateformat.parse dateutil.getdatetime ("Yyyy-mm-dd HH:mm:ss", Getfirstdateinnextmo               Nthof (New Date ()))); (2) Datzxds.setvalidtime (Dateutil.convertstringtodate ("Yyyy-mm-dd HH:mm:ss", Dateutil.getdatetime ("Yyyy-MM-dd HH: Mm:ss ", NewDate ())); 4 Dateformatutils(1) Dateformatutils.format (date, pattern) private String Periodid = Dateformatutils.format (new date (), "yyyymm"); or pattern for yyyy-mm-dd HH:mm:ss or pattern for yyyy-mm-dd HH:mm:ss or Yyyy/mm/dd hh:MM:ss. SSS where ' S ' –millisecond or pattern for yyyy/mm/dd hh:MM:ss. Sss Pattern is and {@link Java.text.SimpleDateFormat} compatible(2) SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); Theform.setvalidtime (New Date ()) (Sdf.format); (3) Dateformatutils.format (Datdxmpinfo.getcreatedate (), "Yyyy-mm-dd HH:mm:ss") 5 DateFormatThe JDK class. SimpleDateFormat is its specific implementation class, which provides a specific implementation class. Public abstract class DateFormatExtends Format also has a number of methods, and its subclasses also provide more methods. DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-i ndependent manner. The Date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date-> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.5.1.1 Get class instances/** * Gets The Date/time formatter with the default formatting style * for the default locale.      * @return a Date/time formatter. */public final static DateFormat Getdatetimeinstance () {return get (default, default, 3, Locale.getdefaul T ()); }5.1.2 Different conversion mechanisms* <p>to format a date for the "current Locale", use one of the * Static factory methods: * <pre> *myString = dateformat.getdateinstance (). Format (mydate);* </pre> * <p>if You are formatting multiple dates, it's * more efficient to get the format and use it mult Iple times so that * the system doesn ' t have to fetch the information about the local * language and country conventions  multiple times.  * <pre> * DateFormat df = dateformat.getdateinstance ();  * for (int i = 0; i < a.length ++i) {* OUTPUT.PRINTLN (Df.format (mydate[i)) + ";");  * </pre> * <p>to format a date for a different Locale, specify it in the * call to Getdateinstance (). * <pre> *DateFormat df = dateformat.getdateinstance (Dateformat.long, locale.france);* </pre> * <p>you can use a dateformat to parse also. * <pre> * mydate = Df.parse (myString);6 DateutilsApache Common lang component 1. Round and TRUNCATE operations on dates 2. The time operation format. A suite of utilities surrounding the use of the * {@link Java.util.Calendar} and {@link java.util.Date} object.</p>7 DateutilUsboss a proprietary class of time. (1) dateutil.convertstringtodate (string amask, String strdate) (2) Date birthday = Dateutil.convertstringtodate (theForm   . Getbirthday ());   (3) Date submittime = null;   Date endsubmittime = null;   Logger.debug ("n = = Test"); try {submittime = dateutil.convertstringtodate ("Yyyy-mm-dd HH:mm:ss", Theform.getsubmittime ());8 CalendarCalendar cal = Calendar.getinstance (); SimpleDateFormat formatter = new SimpleDateFormat ("SSYYYYHHMMDDMM"); String Code=formatter.format (Cal.gettime ());9 Date1. Data.startdate = new Date (Result.gettimestamp ("Start_time"). GetTime ());Ten Apache Common lang components 10.1 dateutils1. Dateutils 1, A suite of utilities surrounding the use of the * {@link Java.util.Calendar} and {@link java.util.Date} ob Ject.</p> 2 provides a mechanism for converting to millis. Provides a number of variables, as well as: how each day is represented.10.2 dateformatutils (most widely used)Introduction: * <p>date and time formatting utilities and constants.</p> * * <p>formatting are performed using th    E * {@link org.apache.commons.lang.time.FastDateFormat} class.</p> 1. are static methods and do not have to be initialized.    2. Format date As String 3. Dateformatutils.format (Date, PatternPrivate String Periodid = Dateformatutils.format (New Date (), "yyyymm"); or pattern for YYYY-MM-DD HH:mm:ssSee also: The Parsepattern method in Fastdateformat. Instance: Dateformatutils.format (New Date (), Yyyy-mm-dd hh:MM:ss. SSS ") 4. Pattern{@link Java.text.SimpleDateFormat} compatible10.3 Fastdateformat1. * <p>fastdateformat is a fast and thread-safe version of * {@link ava.text.simpledateformat}.</p> 2. Fastdateformat the function Parsepattern method to parse, Dateformatutils.format (date,pattern) Form.JDK Time API 11.1 SimpleDateFormat* <code>SimpleDateFormat</code> is aConcrete ClassFor formatting and * parsing dates in a locale-sensitive manner. It allows for formatting * (date-> text), parsing (text-> date), and normalization.The 11.2 Date class is a very powerful class,1, which, cited: DateFormat SimpleDateFormat and so on. ToString has a way of using it in detail. 2, after before compare function, very convenient to use. toLocaleString is obsolete and Dateformat.format (date date) replaces it.12 Project Example 12.1 Get System Current timeDifferent ways to get the system current time. 1. Date date = new Date ();  System.currenttimemillis ()); 2.         Calendar calendar = calendar.getinstance (Locale.chinese); Date commitdate = Calendar.gettime ();              Get system Current Time Logger.debug ("The current time is: =====/n" + commitdate); Datcustomerservice.setcommitdate (commitdate);12.2 The method of increasing and decreasing the month and day of a date. Calendar calendar = Calendar.getinstance ();        Calendar.settime (date);        int month = Calendar.get (calendar.month); Logger.debug ("/n = = The Month is:" + month + 1);        0 represents January, int newmonth = month + 1;        Calendar.set (Calendar.month, newmonth);        Date newdate =calendar.gettime (); return newdate;12.3 Time interval Statistics Tool class1. Long starttime = System.currenttimemillis (); Long elapsedtime = System.currenttimemillis ()-StartTime 2. Org.apache.commons.lang.time.StopWatch 3. Org.springframework.util.StopWatch12.4 Time Issues in the PO on the JSP page If you are using the PO as the bean in JSP, then you can, in the display time, use:     < tr class = "FORM" >       < td width = "20%" class = "FORM" > Accept Person: </td >           < td class = " FORM ">< bean:write name =" Datdxmpinfoobj "property =" Staffno "/></td >      &n bsp;         < td width = "20%" class = "FORM" > Processing time: </td >  &nbsp ;        &l

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.