Java time simple operation, java operation

Source: Internet
Author: User

Java time simple operation, java operation

It's really cool to use java to operate on time, or I am still comfortable with C #, all done with a DateTime

Here, Date refers to java. util. Date.

Get current time:

// Create a Date object of the current time Date time = new Date ();

To increase or decrease the time when the eggs hurt:

// Use the Calendar class to increase or decrease the time. getInstance (); // get a Calendar instance. This class is an abstract class, so you cannot use the new constructor. // use the setTime method to create a time. This time is of the Date type c. setTime (time); // increase the current time by 12 months. You can change the Unit c according to the Calendar enumerated value. add (Calendar. MONTH, 12); // convert Calendar to Date object Date dateTime = c. getTime ();

Once again, the time is formatted to help you see the format:

// Use SimpleDateFormat to format the time String in the format of String timeStr = new SimpleDateFormat ("yyyy-MM-dd hh: mm: ss"). format (time );

 

Simple operation, Memo:

Convenient timestamp conversion:

/*** Convert a time object to a timestamp ** @ param 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 the timestamp into a time object ** @ param time * timestamp * @ return time object */public static Date LongToDate (long time) {Date date Date = null; try {date = new Date (time); System. out. println (date);} catch (Exception e) {e. printStackTrace ();} return date ;}

 

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.