Date of Java

Source: Internet
Author: User

1. Java Time Processing

 Packagecom.bmkit.util.date;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;/*** Created by Root on 16-5-23.*/ Public classTestDate {/*** Format time*/    Private Static voidFormattime () {SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); Date Date=NewDate ();    System.out.println (Dateformat.format (date)); }    /*** Format time*/    Private Static voidformatTime1 (date date) {SimpleDateFormat DateFormat=NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS");    System.out.println (Dateformat.format (date)); }    /*** Parse string date to date*/    Private Static voidparsestringtodate () {String sdate= "2016-05-23 03:07:32"; String sDate1= "2016-05-23"; SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SimpleDateFormat DATEFORMAT1=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Date=NULL; Date Date1=NULL; Try{Date=Dateformat.parse (sdate); Date1=Dateformat1.parse (sDate1); } Catch(ParseException e) {e.printstacktrace ();        } formatTime1 (date);        FormatTime1 (date1);    System.out.println (camparedate (date, date1)); }    /*** Compare Date return-1,0,1*/    Private Static intcamparedate (Date date1, date date2) {returnDate1.compareto (DATE2); }    /*** Get time stamp system.currenttimemillis () This way the fastest, calendar.getinstance (). Gettimeinmillis ()*/    Private Static voidGettimestamp () {//Method OneSystem.out.println (System.currenttimemillis ()); //Method TwoSystem.out.println (Calendar.getinstance (). Gettimeinmillis ()); //Method ThreeSystem.out.println (NewDate (). GetTime ()); }     Public Static voidgettimebydate () {Date Date=NewDate (); DateFormat DF1= Dateformat.getdateinstance ();//date format, accurate to daySystem.out.println (Df1.format (date)); DateFormat DF2= Dateformat.getdatetimeinstance ();//can be accurate to time division secondsSystem.out.println (Df2.format (date)); DateFormat df3= Dateformat.gettimeinstance ();//only shows the secondsSystem.out.println (Df3.format (date)); DateFormat Df4= Dateformat.getdatetimeinstance (Dateformat.full, dateformat.full);//display date, week, afternoon, time (accurate to seconds)System.out.println (Df4.format (date)); DateFormat Df5= Dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long);//display date, last afternoon, time (accurate to seconds)System.out.println (Df5.format (date)); DateFormat Df6= Dateformat.getdatetimeinstance (Dateformat.short, Dateformat.short);//display date, last afternoon, time (accurate to minute)System.out.println (Df6.format (date)); DateFormat Df7= Dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium);//Display date, time (accurate to minute)System.out.println (Df7.format (date)); }     Public Static voidGettimebycalendar () {Calendar cal=calendar.getinstance (); intYear = Cal.get (calendar.year);//Get year        intmonth = Cal.get (calendar.month);//Get Month        intDay = Cal.get (calendar.date);//Get Day        inthour = Cal.get (Calendar.hour);//hours        intminute = Cal.get (Calendar.minute);//points        intSecond = Cal.get (Calendar.second);//seconds        intWeekOfYear = Cal.get (Calendar.day_of_week);//the day of the weekSystem.out.println ("Now Time is: A.D." + year + "years" + (month + 1) + "Month" + Day + "days" + hour + "+ minute +" + "+ Second +" Seconds Week "+weekofyear); }     Public Static voidMain (string[] arg) {//Formattime (); //parsestringtodate ();Gettimestamp (); //gettimebydate (); //Gettimebycalendar ();    }}

2. SimpleDateFormat function Syntax:
G-Age Marker
Y years
M-month
D-Day
h in the morning or afternoon (1~12)
H at the time of Day (0~23)
M min
s S
S ms
E-week
D the first day of the year
F January The first few weeks
W The first few weeks of the year
W January The first few weeks
A morning/afternoon marker
K in one day (1~24)
K in the morning or afternoon (0~11)
Z Time zone
The common standard of writing "Yyyy-mm-dd HH:mm:ss", note case, time is 24 hours, 24-hour conversion to 12-hour system only to change the HH to HH, do not need another function.

Date of Java

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.