Conversion between string and date, timestamp in Java _java

Source: Internet
Author: User
Tags dateformat

One, string and Date (java.util.Date) interchange

1.1 String-> Date

String datestr = "//::";  
    Date date = new Date ();  
   Note format must match the format of date string  
   dateformat sdf = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");  
   try {  
     date = Sdf.parse (DATESTR);  
      System.out.println (Date.tostring ());  
    } catch (Exception e) {  
      e.printstacktrace ();  
    } 
 String datestr = "2010/05/04 12:34:23";
 Date date = new Date ();
 Note format must match the format of date string
 dateformat sdf = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
 try {
  date = Sdf.parse (DATESTR);
  System.out.println (Date.tostring ());
 } catch (Exception e) {
  e.printstacktrace ();
 

1.2 Date-> String

Date to string conversion, you can set any conversion format

String datestr = "";  
   Date date = new Date ();  
   Format can be arbitrary  
    dateformat sdf = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");  
   DateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh/mm/ss");  
    try {  
      datestr = Sdf.format (date);  
     System.out.println (DATESTR);  
      Datestr = Sdf.format (date);  
      System.out.println (DATESTR);  
    } catch (Exception e) {  
      e.printstacktrace ();  
    } 
 String datestr = "";
 Date date = new Date ();
 Format can be arbitrary
 dateformat sdf = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
 DateFormat sdf2 = new SimpleDateFormat ("Yyyy-mm-dd hh/mm/ss");
 try {
  datestr = Sdf.format (date);
  System.out.println (DATESTR);
  Datestr = Sdf2.format (date);
  System.out.println (DATESTR);
 } catch (Exception e) {
  e.printstacktrace ();
 

Two, string and timestamp mutual turn

2.1 String->timestamp

Using the timestamp valueof () method

Timestamp ts = new Timestamp (System.currenttimemillis ());  
    String tsstr = "--::";  
     try {  
       ts = timestamp.valueof (TSSTR);  
       SYSTEM.OUT.PRINTLN (TS);  
    } catch (Exception e) {  
      e.printstacktrace ();  
    } 
 Timestamp ts = new Timestamp (System.currenttimemillis ());
 String tsstr = "2011-05-09 11:49:45";
 try {
  ts = timestamp.valueof (TSSTR);
  SYSTEM.OUT.PRINTLN (TS);
 } catch (Exception e) {
  e.printstacktrace ();
 

Note: String must be of type such as: Yyyy-mm-dd hh:mm:ss[.f ...] in this format, brackets indicate optional, otherwise the error is!!!

If the string is in another format, consider parsing the string again, and then reorganizing ~ ~

2.2 Timestamp-> String

Use Timestamp's ToString () method or borrow DateFormat

Timestamp ts = new Timestamp (System.currenttimemillis ());  
    String tsstr = "";  
    DateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");  
    try {  
      //method one  
      tsstr = Sdf.format (ts);  
     System.out.println (TSSTR);  
     Method two  
    tsstr = ts.tostring ();  
      System.out.println (TSSTR);  
    } catch (Exception e) {  
     e.printstacktrace ();  
   } 
 Timestamp ts = new Timestamp (System.currenttimemillis ());
 String tsstr = "";
 DateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
 try {
  //method one
  tsstr = Sdf.format (ts);
  System.out.println (TSSTR);
  Method two
  tsstr = ts.tostring ();
  System.out.println (TSSTR);
 } catch (Exception e) {
  e.printstacktrace ();
 

It is easy to see that the advantage of method one is that it is flexible to set the form of a string.

Iii. Date (java.util.Date) and timestamp interchange

Statement: Check API, date and Timesta are parent-child relationships

3.1 Timestamp-> Date

Timestamp ts = new Timestamp (System.currenttimemillis ());  
   Date date = new Date ();  
   try {  
     date = ts;  
      SYSTEM.OUT.PRINTLN (date);  
    catch (Exception e) {  
     e.printstacktrace ();  
    } 
 Timestamp ts = new Timestamp (System.currenttimemillis ());
 Date date = new Date ();
 try {
  date = ts;
  SYSTEM.OUT.PRINTLN (date);
 catch (Exception e) {
  e.printstacktrace ();
 }

Very simple, but at the moment the date object points to an entity that is a timestamp, that is, date has a method of the date class, but the executing entity of the overridden method is in timestamp.

3.2 Date-> Timestamp

The parent class cannot be directly converted to subclasses, with the help of the middle string~~~~

Java.sql.Date only store date data does not store time data
//will lose time Data
preparedstatement.setdate (1, New Java.sql.Date (Date.gettime ()));
This can be done with
Preparedstatement.settimestamp (1, New Java.sql.Timestamp (New Java.util.Date (). GetTime ()));
 Want to get complete data, including date and time, you can do so
java.util.Date d = resultset.gettimestamp (1);
This is a more appropriate process to avoid some potential timestamp problems
java.util.Date d = new Java.util.Date (Resultset.gettimestamp (1). GetTime ());

Make up your own words, such words:

When storing to the database, you can receive the Java.util.Date type and then use the GetTime () method to get a long value representing that date object, and then construct a timestamp object into the database with this long value.

When you take it from a stored database, you can get timestamp to use his gettime () method to get a long value and then construct a Java.util.Date object with this long value so that you can manipulate the date object. It's better to say new Simpletimeformat ("Yyyyy-mm-dd HH:mm:ss"). Format () and so on

Related Article

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.