Java datetime date type, long type, string representation conversion (RPM)

Source: Internet
Author: User
Tags days in month

Java datetime date type, long type, conversion of string type representation

1. Convert java.util.Date type to long type
Java.util.Date dt = new Date ();
System.out.println (Dt.tostring ()); The meaning of Java.util.Date
Long lSysTime1 = Dt.gettime ()/1000; Gets the number of seconds, the date type of gettime () returns the number of milliseconds

2. Convert from long to date type
SimpleDateFormat sdf= New SimpleDateFormat ("mm/dd/yyyy HH:mm:ss");
The previous lsystime is the number of seconds, multiplying by 1000 to get the number of milliseconds, and then to the Java.util.Date type
Java.util.Date dt = new Date (LSYSTIME1 * 1000);
String sdatetime = sdf.format (DT); Get a precise representation of the second: 08/31/2006 21:08:00
System.out.println (Sdatetime);

3. "08/31/2006 21:08:00" format string conversion java.util.Date type
String sDt = "08/31/2006 21:08:00";
SimpleDateFormat sdf= New SimpleDateFormat ("mm/dd/yyyy HH:mm:ss");
Date DT2 = Sdf.parse (sDt);
Continue conversion to get the number of seconds of the long type
Long ltime = Dt2.gettime ()/1000;


Note that HH and HH are different, hh means to take 24-hour production, hh means to take 12-hour production
The meaning of the commonly used format, excerpted from the JDK, note that the meaning of the case is usually different:
Example of letter meaning
Y years 1996;96 which year
M Month in year J uly; jul;07 which month of the year
M Minute in hour 301 hours of the first minute
W Week in the first few weeks of year 271
W Week in month the first few weeks of 21 months
Day of the year 1891
Days in month of 101 months
H Hour in Day (0-23) hours in 1 days (24-hour system)
H Hour in am/pm (1-12) hours of the morning and afternoon of 121 days (12-hour clock)
S millisecond of 978 milliseconds
S Second in minute 551 minutes of the first few seconds

4, the current time of the system
Long lSysTime2 = System.currenttimemillis (); Gets the current time of the system in milliseconds

Original: http://www.blogjava.net/algz/articles/324727.html

Java datetime date type, long type, string representation conversion (RPM)

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.