Java Time Simple operation

Source: Internet
Author: User

Use Java operation time feeling real egg hurts, or My big C # comfortable, a datetime all done

The Date here refers to Java.util.Date.

Get current time:

        Creates a date object of the current time        date times = new Date ();

Where the egg hurts, increase and decrease the operation of the time:

        Use the Calendar class to increase or decrease the time of the        calendar C = calendar.getinstance ();//To obtain a calendar instance, which is an abstract class, so you cannot use the new constructor method        // Use the SetTime method to create a time, which is the date type        c.settime.        Adds 12 months to the current time, which can be changed to add Unit        c.add (Calendar.month, +) According to the Calendar enumeration value;        Convert Calendar to date object        Date DateTime = C.gettime ();

Again the place where the egg hurts, format the time, convenient for people to look at the format:

        Use SimpleDateFormat to format the time as a string        timestr = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (timing);

Simple operation, Memo:

Convenient timestamp conversions:

    /**     * Turn time objects into timestamps     * @param time     * time * @return Timestamp *            /public    static Long Datetolong ( Date time) {        try {            long Timel = Time.gettime ();            System.out.print (Timel);            return Timel;        } catch (Exception e) {            e.printstacktrace ();            return 0;        }    }    /**     * Convert timestamp to time object     *      * @param time *     timestamp     * @return Time Object    */public static Date Longtodate (long time) {        date date = null;        try {            date = new Date (time);            SYSTEM.OUT.PRINTLN (date);        } catch (Exception e) {            e.printstacktrace ();        }        return date;    }

Java Time Simple operation

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.