Calculation and conversion of time and date in Java

Source: Internet
Author: User
Tags time and date

In Java, the date and calendar two classes are the main representation of time, and the SimpleDateFormat class used for date and string conversions.

  • Conversion between 0x00:date and long type
      Java internally uses UTC time, which holds the millisecond value experienced since January 01, 1970 00:00 00 seconds, using a long type.
      The Date class import Java.util.date;long Millis = 0;date date = new Date () in the Java.util package needs to be imported;//convert date to Millismillis = Date.gettime ();//convert Millis to Datedate = SetTime (Millis);
  • conversion between 0x01:date and string types
      the format output and format input of the date type need to be used to SimpleDateFormat class
      import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date; SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//date converted to string in the format specified by SDF, using the SDF Format method Date Date = New Date (); String str = sdf.format (date),//string to date, using the Parse method, requires exception handling str = "2014-11-21 10:47:28"; try {date = Sdf.parse (str);} C 
       Atch (ParseException e) {e.printstacktrace ();} 
  • Conversion between 0x02:date and calendar
      Most of the methods in date are obsolete, and date calculations mainly use the calendar.
      Import Java.util.calendar;import Java.util.date;//calendar is an abstract class that cannot be directly instantiated, and can return an object of a subclass through its static method getinstance (). Calendar calendar = calendar.getinstance ();//Use the GetTime method to convert the calendar to datedate date = Calendar.gettime ();// Use the SetTime method to convert date to calendarcalendar.settime (date);

Calculation and conversion of time and date in Java

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.