Conversion between String and Date Timestamp in Java

  • 2020-04-01 04:34:50
  • OfStack

First, String and Date (java.util.Date) turn to each other

        1.1 the String - > The Date


String dateStr = "// ::";  
    Date date = new Date();  
   //Note that the format matches the format of the 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 that the format matches the format of the 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 the Date - > The String

    Date to the string conversion, you can set any conversion format


String dateStr = "";  
   Date date = new Date();  
   //The format of the 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();
 //The format of the 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();
 } 

  Ii. Mutual transfer of String and Timestamp

  2.1 the String - > Timestamp

    Use the valueOf() method of Timestamp


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: the type of String must be shaped like: yyyy-mm-dd hh:mm:ss[. F...] In this format, the middle bracket indicates optional, otherwise report an error!!

      If String is in a different format, consider reparsing the String and regrouping ~~

      Timestamp - 2.2 > The String

  Use the Timestamp toString() method or borrow the DateFormat


Timestamp ts = new Timestamp(System.currentTimeMillis());  
    String tsStr = "";  
    DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  
    try {  
      //Methods a
      tsStr = sdf.format(ts);  
     System.out.println(tsStr);  
     //Method 2
    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 {
  //Methods a
  tsStr = sdf.format(ts);
  System.out.println(tsStr);
  //Method 2
  tsStr = ts.toString();
  System.out.println(tsStr);
 } catch (Exception e) {
  e.printStackTrace();
 } 

  It is easy to see that the advantage of method 1 is that you can set the form of the string flexibly.

Date (java.util.Date) and Timestamp are exchanged

  Declaration: look up the API to see that Date and Timesta are parent-child classes

  Timestamp - 3.1 > The 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();
 }

  Simple, but at this point the entity that the date object points to is a Timestamp, meaning that date has methods of the date class, but the executing entity of the overridden method is in Timestamp.

    3.2 the Date - > Timestamp

    The parent class cannot be converted directly to the subclass, but can be converted with String~~~~ in the middle


java.sql.Date  Only date data is stored, not time data 
//You lose time data
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));
//You can do it this way
preparedStatement.setTimestamp(1, new java.sql.Timestamp(new java.util.Date().getTime()));
 //For complete data, including date and time, go like this
java.util.Date d = resultSet.getTimestamp(1);
//This is more appropriate to avoid some potential Timestamp problems
java.util.Date d = new java.util.Date(resultSet.getTimestamp(1).getTime());

Make it up on your own, like this:

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

            When fetching from the saved database, you can get the Timestamp with his getTime() method to get the long value, and then construct a java.util.Date object with the long value, so that you can operate on the Date object. Instead, say new SimpleTimeFormat(" yyyyy-mm-dd HH: MM :ss"). Format(), etc


Related articles: