java-Common time format conversion

Source: Internet
Author: User

 PackageCom.test.demo;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date; Public classDataformatexamples { Public Static voidMain (string[] args) {String pattern_yyyymmdd= "Yyyy-mm-dd"; String Pattern_yyyymmddhhmmss= "Yyyy-mm-dd HH:mm:ss"; SimpleDateFormat SDF_YYYYMMDD=NewSimpleDateFormat (PATTERN_YYYYMMDD); SimpleDateFormat Sdf_yyyymmddhhmmss=NewSimpleDateFormat (PATTERN_YYYYMMDDHHMMSS); String strDate1= "2017-12-01"; String StrDate2= "2017-12-01 15:38:26"; String StrDate3= "2018-01-05 11:15:13"; //1. Current system time conversion (date-->string)System.out.println (Sdf_yyyymmdd.format (NewDate ())); System.out.println (Sdf_yyyymmddhhmmss.format (NewDate ())); //2. String conversion to TIME type        Try{System.out.println (Sdf_yyyymmdd.parse (strDate1));        System.out.println (Sdf_yyyymmddhhmmss.parse (StrDate2)); } Catch(ParseException e) {e.printstacktrace (); }                //3. Yesterday/Tomorrow/last month/next month/last year/next year's dateCalendar Yesday =calendar.getinstance (); Calendar Tomday=calendar.getinstance (); Calendar Lastmon=calendar.getinstance (); Calendar Nextmon=calendar.getinstance (); Calendar LastYear=calendar.getinstance (); Calendar Nextyear=calendar.getinstance (); Yesday.add (Calendar.date,-1); Tomday.add (Calendar.date,1); Lastmon.add (Calendar.month,-1); Nextmon.add (Calendar.month,1); Lastyear.add (Calendar.year,-1); Nextyear.add (Calendar.year,1); Date Yesdayt=Yesday.gettime (); Date Tomdayt=Tomday.gettime (); Date Lastmont=Lastmon.gettime (); Date Nextmont=Nextmon.gettime (); Date Lastyeart=Lastyear.gettime (); Date Nextyeart=Nextyear.gettime (); System.out.println (Sdf_yyyymmddhhmmss.format (Yesdayt));//YesterdaySystem.out.println (Sdf_yyyymmddhhmmss.format (Tomdayt));//TomorrowSystem.out.println (Sdf_yyyymmddhhmmss.format (Lastmont));//last monthSystem.out.println (Sdf_yyyymmddhhmmss.format (Nextmont));//next monthSystem.out.println (Sdf_yyyymmddhhmmss.format (Lastyeart));//last yearSystem.out.println (Sdf_yyyymmddhhmmss.format (Nextyeart));//next year//4, two days difference between dates        Try {            //① by 24 hours for one daySystem.out.println (Sdf_yyyymmddhhmmss.parse (strDate3). GetTime () -Sdf_yyyymmddhhmmss.parse (StrDate2). GetTime ())/((1000*3600*24))); //② Ignore time only count dateSystem.out.println (Sdf_yyyymmdd.parse (strDate3). GetTime () -Sdf_yyyymmdd.parse (StrDate2). GetTime ())/((1000*3600*24))); } Catch(ParseException e) {e.printstacktrace (); }            }}

java-Common time format conversion

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.