String, Timestamp, date in Java are converted to each other

Source: Internet
Author: User
Tags dateformat

Timestamp converted to string:

SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//defines the format, does not display millisecondsTimestamp now = new Timestamp (System.currenttimemillis ());//Get system Current timeString str = Df.format (now); Convert string to timestamp:SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String time = Df.format (new Date ());Timestamp ts = timestamp.valueof (time);Note: When the time in the form does not have a second, the string should be converted to a date type and then converted to timestamp. The code is as follows:SimpleDateFormat df = new Simple ("Yyyy-mm-dd hh:mm");Date date = Df.parse (String);Timestamp ts = new Timestamp (Date.gettime ()); conversion between Date, String, timestamp!

Conversion between Date and string the main function:
public static void Main (string[] args) {
TODO auto-generated Method Stub
DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = null;
String str = NULL;

//String to date
str = "2009-01-06";
try {
Date = Format.parse (str); Wed Sep 00:00:00 CST 2007
} catch (ParseException e) {
E.printstacktrace ();
}

Date = Java.sql.Date.valueOf (str); Only the date part is kept, returning the Java.sql.Date 2007-9-26
SYSTEM.OUT.PRINTLN (date);


//Date Turn string
Date = new Date (); Wed Sep 17:14:01 CST 2007
str = Format.format (date); 2007-9-26
System.out.println (str);
Format = dateformat.getdateinstance (Dateformat.short);
str = Format.format (date); 07-9-26
System.out.println (str);

Format = dateformat.getdateinstance (Dateformat.medium);
str = Format.format (date); 2007-9-26
System.out.println (str);
Format = dateformat.getdateinstance (dateformat.full);
str = Format.format (date); Wednesday, September 26, 2007
System.out.println (str);
}

" function converted between timestamp and String:
public static void main (string[] args) {
   //TODO auto-generated method stub
   //timestamp converted to string:
    simpledateformat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//define format, do not display milliseconds
     timestamp now = new Timestamp (System.currenttimemillis ());//Get system Current time
     String str = Df.format (now);
    system.out.println (str);
    
    ///string conversion to timestamp:
     SimpleDateFormat df1 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    date date = new Date ();
    string time = Df1.format (date);
    timestamp ts = timestamp.valueof (time);
    system.out.println (TS);

}

String, Timestamp, date in Java are converted to each other

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.