The interaction between Java date and long,string and date size comparison

Source: Internet
Author: User
Tags date1

Summarize the conversion and comparison of date types

1.date-> String

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");

String Date=sdf.format (new Date ());

2.string->date

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");

Date date = Sdf.parse (datestring);

3.date->long

Java.util.Date dt = new Date ();
Long lSysTime1 = Dt.gettime ()/1000; Gets the number of seconds, the date type gettime () returns the number of milliseconds

4.long->date

SimpleDateFormat sdf= New SimpleDateFormat ("mm/dd/yyyy HH:mm:ss");
The previous lsystime is the number of seconds, first multiply by 1000 to get the number of milliseconds, then to java.util.Date type
Java.util.Date dt = new Date (LSYSTIME1 * 1000);
String sdatetime = sdf.format (DT); Get Accurate to SEC representation: 08/31/2006 21:08:00


5. Date type comparison

Date Date1=new date ();

Date Date2=new date ();

if (Date1.gettime () >date2.gettime ())

{

System.out.print ("Date1 greater than Date2");

}



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.