Java date and time, and mutual conversions

Source: Internet
Author: User
Tags date1 set time

Java date and time, and mutual conversions

1  Packagecom.study.string;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 ImportJava.util.Calendar;6 Importjava.util.Date;7 ImportJava.util.GregorianCalendar;8 9  Public classDatebase {Ten  One      Public Static voidMain (string[] args)throwsParseException { A         /* - * Get current time -          */ theDate date1 =NewDate (); -         LongLong1 = Date1.gettime ();//date type, converted to long type -System.out.println (date1);//Sat 08:36:36 gmt+08:00 -System.out.println (LONG1);//1503708031359 +          -Calendar Cale1 =calendar.getinstance (); +Date1 = Cale1.gettime ();//Calendar type to date type ALong1 =date1.gettime (); at System.out.println (cale1); -System.out.println (date1);//Sat 08:36:36 gmt+08:00 - System.out.println (long1); -          -         /* - * Set Time in          */ -Long1 + = 24*60*60*1000; to Date1.settime (long1); +System.out.println (date1);//Sun 08:43:26 gmt+08:00 -          the         /* * * Format time date, no parameter default format, parameter custom format.  $ * Date-String with Format ()Panax Notoginseng * String-Date with parse () -          */ theSimpleDateFormat sim1 =NewSimpleDateFormat ();//default format: 17-8-27 am 8:45 +String time1 =Sim1.format (date1); ASystem.out.println (TIME1);//17-8-27 Morning 8:45 theDate Date11 =Sim1.parse (time1); + System.out.println (DATE11); -          $SimpleDateFormat sim2 =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss sss"); $String time2 =Sim2.format (date1); -System.out.println (time2);//2017-08-27 08:47:58 058 -Date date22=Sim2.parse (time2); theSystem.out.println (DATE22);//Sun 08:52:08 gmt+08:00 -         Wuyi         /* the * Create a specified date String - * GregorianCalendar Wu          */ -SimpleDateFormat sim3 =NewSimpleDateFormat ("Yyyy-mm-dd"); AboutString str1 = "2014-09-27"; $Date date33 =Sim3.parse (str1); -System.out.println (DATE33);//Sat Sep 00:00:00 gmt+08:00 -          -          AGregorianCalendar Gre1 =NewGregorianCalendar (2015,calendar.december,25); +Date date44 =gre1.gettime (); theSystem.out.println (DATE44);//Fri Dec 00:00:00 gmt+08:00 -          $          theCalendar Cal2 =calendar.getinstance (); theCal2.set (Calendar.year, 2017); theCal2.set (Calendar.month, 7);//month's number is 1, 8 = = Calendar.september theCal2.set (Calendar.date, 26);//Tue Sep 09:04:25 gmt+08:00 - //Cal2.set (Calendar.day_of_month, n); inSystem.out.println (Cal2.gettime ());//Sat 09:09:44 gmt+08:00 the          the          About          the         /* the * Get date, week, time the          */ +         intDayOfweek =Cal2.get (calendar.day_of_week); -System.out.println (DayOfweek);//7 is Saturday the         Bayi          the         /* the * Calendar time plus minus -          */ -Calendar Cal3 =calendar.getinstance (); theCal3.add (Calendar.year, 1); theCal3.add (Calendar.month, 2); theSystem.out.println (Cal3.gettime ());//Tue June 09:14:11 gmt+08:00 2018 the          -          the         /* the          *  the          */94Calendar Cal4 =calendar.getinstance (); theCal4.set (Calendar.year, 2016); theCal4.set (Calendar.date, 1); the         //the last day of every month98          for(intmonth = calendar.january;month<calendar.december; month++){ About Cal4.set (Calendar.month, MONTH); -System.out.println (Cal4.get (calendar.year) + "year" + (month+1) + "month" +101Cal4.getactualmaximum (calendar.date) + "Day");102}/*103 January 31, 2016104 February 29, 2016 the March 31, 2016106 April 30, 2016107 May 31, 2016108 June 30, 2016109 July 31, 2016 the August 31, 2016111 September 30, 2016 the October 31, 2016113 November 30, 2016 the          */ the          the         //time to create a long type directly117         LongLong2 =System.currenttimemillis ();118Date Daten =NewDate (long2);119System.out.println (Daten);//Sat 09:41:08 gmt+08:00 -     }121 122}

Java date and time, and mutual conversions

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.