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