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 New Date ();

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

        //  Use the Calendar class for time increment, subtraction operations        Calendar C = calendar.getinstance (); // get a calendar instance, which is an abstract class so you cannot use the new construct        method // use the SetTime method to create a time that is a date type         C.settime (time);         // Adds 12 months to the current time, which can be changed to add units        based on the Calendar enumeration value C.add (Calendar.month,N);         // 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 time as a string        New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"). Format (time);

Simple operation, Memo:

Convenient timestamp conversions:

    /*** Turn the time object into a timestamp * *@paramTime * *@returntime Stamp*/     Public Static LongDatetolong (Date time) {Try {            LongTimel =Time.gettime ();            System.out.print (Timel); returnTimel; } Catch(Exception e) {e.printstacktrace (); return0; }    }    /*** Convert timestamp to TIME object * *@paramtime * Timestamp *@returnTime Object*/     Public StaticDate Longtodate (LongTime ) {Date Date=NULL; Try{Date=NewDate (time);        SYSTEM.OUT.PRINTLN (date); } Catch(Exception e) {e.printstacktrace (); }        returndate; }

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.