Summary of common methods in Calendar in Java

Source: Internet
Author: User

1 //get current moment yyyy-mm-dd HH:mm:ss2Calendar Calendar =calendar.getinstance ();3SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format4String now =Sdf.format (Calendar.gettime ());5 System.out.println (now);6 7 //get current moment yyyy-mm-dd8Calendar Calendar =calendar.getinstance ();9SimpleDateFormat SDF1 =NewSimpleDateFormat ("Yyyy-mm-dd");//Set Date formatTenString Now1 =Sdf1.format (Calendar.gettime ()); One System.out.println (NOW1); A  - //get current moment yyyy-mm -Calendar Calendar =calendar.getinstance (); theSimpleDateFormat SDF2 =NewSimpleDateFormat ("yyyy-mm");//Set Date format -String Now2 =Sdf2.format (Calendar.gettime ()); - System.out.println (now2); -  + //get current moment, Month day -Calendar Calendar =calendar.getinstance (); + intYear =Calendar.get (calendar.year); A intmonth = Calendar.get (calendar.month) + 1; at intDay =Calendar.get (calendar.day_of_month); -SYSTEM.OUT.PRINTLN (Year + "-" + month + "-" +Day ); -  - //gets the current time timestamp -Calendar Calendar =calendar.getinstance (); - LongTS =Calendar.gettimeinmillis (); in System.out.println (TS); -  to //get last month yyyy-mm +Calendar Calendar =calendar.getinstance (); -Calendar.add (Calendar.month, 1); theString Lastmonth =NewSimpleDateFormat ("yyyy-mm"). Format (Calendar.gettime ()); * System.out.println (lastmonth); $ Panax Notoginseng //Get yesterday Yyyy-mm-dd -Calendar Calendar =calendar.getinstance (); theCalendar.add (Calendar.date, 1); +String yesterday =NewSimpleDateFormat ("Yyyy-mm-dd"). Format (Calendar.gettime ()); A System.out.println (yesterday); the  + //converts a specified month and minute to a timestamp -SimpleDateFormat SimpleDateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); $Date date = Simpledateformat.parse ("2015-05-01 00:00:00"); $ LongTs1 =date.gettime (); - System.out.println (TS1); - //or theCalendar Calendar =calendar.getinstance (); -Calendar.set (2015, 4, 1, 0, 0, 0);//here 4 means MayWuyi LongTS2 =Calendar.gettimeinmillis (); the System.out.println (TS2); -  Wu //Converts the specified timestamp to year-month day-time seconds -SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss" ); About LongTS = 1234567899L; $String date =Format.format (TS); -Date date1 =format.parse (date); -System.out.println ("Format to String (Date):" +date); -System.out.println ("Format to Date:" + date1);

Summary of common methods in Calendar in 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.