Java Date Processing

Source: Internet
Author: User
Tags dateformat local time

first, related categories

1. Date:
Date represents a specific moment, accurate to milliseconds, and the corresponding method in date is deprecated, starting with JDK 1.1, you should use the Calendar class to implement conversion between date and Time fields.
2, DateFormat:
DateFormat is an abstract class of date/time formatting subclasses that formats and resolves dates or times, which can be converted by date, text, and date. Sub-class SimpleDateFormat.
3. Calendar:
Calendar is an abstract class of calendars that can be used to get datetime. Recommended use.

 PackageDatetext;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classText { Public Static voidMain (string[] args) {Date d=NewDate (); System.out.println (d);//the first method of representation, Sat 20:18:10 CSTSystem.out.println ("----------------------"); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println (Sdf.format (d));//the second method of presentation, 2017-07-22 20:18:10| The use of SimpleDateFormatSystem.out.println ("----------------------");
//old ways, slowly being eliminated.System.out.println (D.gettime ()); System.out.println (D.getyear ()); System.out.println (D.getmonth ()+1); System.out.println (D.getdate ()); System.out.println (D.gethours ()); System.out.println (D.getminutes ()); System.out.println (D.getseconds ()); System.out.println ("----------------------"); //new method, with calendarCalendar C =calendar.getinstance (); //calendar.getinstance () gets a calendar using the default time zone and locale. System.out.println (C.get (calendar.year)); System.out.println (C.get (calendar.month)+1);//Note: The month requires +1 to indicate the current monthSystem.out.println (C.get (calendar.date)); System.out.println (C.get (Calendar.hour)); System.out.println (C.get (Calendar.minute)); System.out.println (C.get (Calendar.second)); System.out.println ("----------------------"); //SimpleDateFormatDate D2 =NewDate (); SimpleDateFormat SDF2=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println (Sdf2.format (D2)); Calendar C2=calendar.getinstance (); Date d_cal=C2.gettime (); System.out.println (Sdf2.format (d_cal)); }}

Second, SimpleDateFormat

SimpleDateFormatis a specific class that formats and parses dates in a language-related manner. It allows formatting (date-and text), parsing (text-to-date), and normalization.

SimpleDateFormatAllows you to select any user-defined pattern of date-time formats. However, it is still recommended DateFormat getTimeInstance getDateInstance getDateTimeInstance to create a date-time formatter through, or to, the new.

1. Date and Time mode

2, Example: Given the date and time of the United States Pacific Time zone local time 2001-07-04 12:08:56

Iii. International time (with calendar)
 PackageDatetext;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.TimeZone; Public classText { Public Static voidMain (string[] args) {Date d=NewDate (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");        System.out.println (Sdf.format (d)); System.out.println ("----------------------"); /*string[] ids = Timezone.getavailableids (); for (int i=0;i<ids.length;i++) {System.out.println (ids[i]);//output all region ID}*/                //take Japan as an exampleCalendar C = calendar.getinstance (Timezone.gettimezone ("Japan"));        //gettimezone (), Get time zone  System.out.println (C.get (Calendar.hour)); //12-hour systemSystem.out.println (C.get (calendar.hour_of_day));//24-hour system    }}

Four, set the long integer to a specific point in time:
 PackageDatetext;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classText { Public Static voidMain (string[] args) {Date d=NewDate (); //GetTime (), which returns a Date object that represents this calendar time value, from the epoch to the current millisecond offset. System.out.println (D.gettime ());//the output value is: 1500733409509Calendar C=calendar.getinstance (); C.settimeinmillis (1500733409509L);//Settimeinmillis (Long Millis),//sets the current time value for this Calendar with the given long value. Date DD=C.gettime (); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SYSTEM.OUT.PRINTLN (Sdf.format (DD)); //the output value is 2017-07-22 22:23:29    }}
v. Timestamp (timestamp, can be accurate to milliseconds)
 PackageDatetext;ImportJava.sql.Timestamp;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classText { Public Static voidMain (string[] args) {Date d=NewDate (); System.out.println (D.gettime ());//the output value is: 1500733409509Calendar C=calendar.getinstance (); C.settimeinmillis (1500733409509l); Date DD=C.gettime (); Timestamp TS=NewTimestamp (1500733409509l); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS"); System.out.println (Sdf.format (TS)); //the output value is 2017-07-22 22:23:29:509    }}

Java Date Processing

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.