A good memory is better than a bad pen. 2-java Date Type and conversion

Source: Internet
Author: User


1 , System Current time
Long SysTime =system.currenttimemillis (); Gets the current time of the system in milliseconds

datemydate = new Date ();// Get current Time

2. The java.util.Date type converts to a long type of seconds
java.util.Date dt = Newdate ();
Long sysTime = Dt.gettime ()/1000; //get the number of seconds, DateType of gettime () returns the number of milliseconds

3. Convert from long to date type
SimpleDateFormat sdf= newsimpledateformat ("mm/dd/yyyy HH:mm:ss");
//if it's a second, take the firstGets the number of milliseconds, if the millisecond is used directly, and then into the java.util.Date type
Java.util.Date dt = new Date (sysTime * 1000); 
String Sdatetime = sdf.format (DT); //get a precise representation of the second: 08/31/2006 21:08:00


3 , 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);


4 , date dates to strings string

Turn today's date into a string

SimpleDateFormat sdf= Newsimpledateformat ("mm/dd/yyyy HH:mm:ss");

String Datestr=sdf.format (New Date ());

5 , reference

// note that the HH and hh meanings are different, hh means to take 24-hour production, hh means to take 12-hour production
The meaning of the commonly used format, excerptedfrom the JDK, note that the meaning of the case is usually different:

Letters   meaning                           Example
Y year 1996;96which year
M Month in year J uly; Jul;07which month of the year
M Minute Inhourthe first few minutes in one hours
W Week inyearthe first few weeks of the year
W Week inmonth 2the first few weeks of the one months
D Day Inyear 189the day ordinal of a year
D Day Inmonththe day of the one month
H Hour in Day (0-23) 0the first hours of the day ( -hour system)
H Hour in am/pm (1-12)the first hours of the morning and afternoon of the day ( Ahour system)
S Millisecond 978Number of milliseconds
s Second inminutethe first few seconds of a minute

A good memory is better than a bad pen. 2-java Date Type and conversion

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.