Summary of Java time and date processing

Source: Internet
Author: User

Java program modules usually have their own public date-time processing classes, mainly implemented such as:

Time format, get the current date before and after a time, date time comparison and other functions;

1. Date formatting

Date date = new Date ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String StartDate = Format.format (date); Get the formatted string

2. Date increase or decrease

Date date = new Date ();
SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Calendar C = calendar.getinstance ();
C.settime (date);
C.add (Calendar.day_of_year, 7); The current time back 7 days, if you want to use 7 days before the current time-7

Format.format (C.gettime ())

3. Some other uses

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmss", Locale.simplified_chinese); Add local time zone settings

4. It is often necessary to add a date-time processing public class to handle

Like what:

   /**
     * Get the number of seconds from 1970-1994
     * @param date Time
     * @return Count value
     */
    public static long getsecondfrom70to94 ()
    {
        Calendar cs = Calendar.getinstance ();
        Cs.set (Calendar.year, 1970);
        Cs.set (calendar.month, 1);
        Cs.set (calendar.day_of_month, 1);
        cs.set (calendar.hour_of_day, 0);
        cs.set (calendar.minute, 0);
        cs.set (calendar.second, 0);
        cs.set (calendar.millisecond, 0);

Calendar CE = calendar.getinstance ();
Ce.set (Calendar.year, 1994);
Ce.set (Calendar.month, 1);
Ce.set (Calendar.day_of_month, 1);
Ce.set (calendar.hour_of_day, 0);
Ce.set (calendar.minute, 0);
Ce.set (Calendar.second, 0);
Ce.set (Calendar.millisecond, 0);
Return (long) (Ce.gettimeinmillis ()-Cs.gettimeinmillis ());
}




Summary of Java time and date processing

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.