Java date-time Regular expression Java datetime operation method

Source: Internet
Author: User
Tags regular expression

1. Get Calendar instance: Calendar c = calendar.getinstance ();

2. Define date/Time format: SimpleDateFormat SDF =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

3. Convert date/Time to a fixed format using the SimpleDateFormat format () method:

String datetime = Sdf.format (C.gettime ());

4. Converts the string to date/time using the SimpleDateFormat Parse () method: Date D = Sdf3.parse ("2016-08-08 16:43:00");

5. Date/time increase, decrease, use the Calendar Add () method, such as reducing the date by 100 days: C.add (calendar.date,-100);

6. Date/Time settings, using the calendar's set () method, such as setting the hour to 0 o'clock:

C.set (calendar.hour_of_day, 0);

Example:

Package MyCalendar;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date; public class MyCalendar {public static void main (String args[]) throws exception{Calendar C = calendar.getinstance  
    ();
    SimpleDateFormat sdf1 =new SimpleDateFormat ("YyyyMMdd");
    SimpleDateFormat sdf2 =new SimpleDateFormat ("Hhmmss");
    SimpleDateFormat sdf3 =new SimpleDateFormat ("Yyyymmddhhmmss");    
    SimpleDateFormat sdf4 =new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    String date = Sdf1.format (C.gettime ());
    SYSTEM.OUT.PRINTLN (date);
    String time = Sdf2.format (C.gettime ());  
    System.out.println (time);
    String dt = "20160808162405";
    Date d = sdf3.parse (DT);  
    DT = Sdf4.format (d);
    C.settime (d);
    C.add (calendar.date,-100);
    C.set (calendar.hour_of_day, 0);
    C.set (calendar.minute, 0);
    C.set (Calendar.second, 0);
    SYSTEM.OUT.PRINTLN ("100 days ago:" + Sdf4.format (C.gettime ())); C.add (Calendar.DATE, 200);
    C.set (calendar.hour_of_day, 0);
    C.set (calendar.minute, 0);
    C.set (Calendar.second, 0);
  SYSTEM.OUT.PRINTLN ("100 Days later:" + Sdf4.format (C.gettime ())); }
}

The above is the Java date time operation method of all the contents of the introduction, I hope to help.

Related Article

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.