Conversion between java:string and date, timestamp

Source: Internet
Author: User
Tags dateformat

Turn http://www.cnblogs.com/Matrix54/archive/2012/05/03/2481260.html

One, string and Date (java.util.Date) each other

1.1 String--Date

  1. String datestr = "2010/05/04 12:34:23";
  2. Date date = new Date ();
  3. //Note format is formatted to match the format of the date string
  4. DateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
  5. try {
  6. Date = Sdf.parse (DATESTR);
  7. System.out.println (Date.tostring ());
  8. } catch (Exception e) {
  9. E.printstacktrace ();
  10. }

1.2 Date, String

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

  1. String datestr = "";
  2. Date date = new Date ();
  3. the//format format can be arbitrarily
  4. DateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
  5. DateFormat SDF2 = new SimpleDateFormat ("Yyyy-mm-dd hh/mm/ss");
  6. try {
  7. Datestr = Sdf.format (date);
  8. System.out.println (DATESTR);
  9. Datestr = Sdf2.format (date);
  10. System.out.println (DATESTR);
  11. } catch (Exception e) {
  12. E.printstacktrace ();
  13. }

Two, string and timestamp mutual transfer

2.1 String->timestamp

Using the timestamp valueof () method

  1. Timestamp ts = new Timestamp (System.currenttimemillis ());
  2. String tsstr = "2011-05-09 11:49:45";
  3. try {
  4. ts = timestamp.valueof (TSSTR);
  5. SYSTEM.OUT.PRINTLN (TS);
  6. } catch (Exception e) {
  7. E.printstacktrace ();
  8. }

Note: The type of string must be a shape such as:yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错!!!

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

2.2 Timestamp, String

Use the ToString () method of timestamp or borrow DateFormat

  1. Timestamp ts = new Timestamp (System.currenttimemillis ());
  2. String tsstr = "";
  3. DateFormat SDF = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");
  4. try {
  5. //Method one
  6. TSSTR = Sdf.format (ts);
  7. System.out.println (TSSTR);
  8. //Method two
  9. Tsstr = Ts.tostring ();
  10. System.out.println (TSSTR);
  11. } catch (Exception e) {
  12. E.printstacktrace ();
  13. }

It is easy to see that the advantage of method one is that it can be flexibly set in the form of strings.

Third, Date (java.util.Date) and timestamp mutual transfer

Disclaimer: The API indicates that date and Timesta are parent-child relationships

3.1 Timestamp-Date

    1. timestamp ts = new timestamp ( System.currenttimemillis ());   
    2.         date date = new date ();   
    3.         try {   
    4.             date  = ts;   
    5.              SYSTEM.OUT.PRINTLN (date);   
    6.         } catch  ( exception e)  {  
    7.              e.printstacktrace ();   
    8.         }  

Very simple, but now the entity that the date object points to is a timestamp, that is, date has a method of the date class, but the execution entity of the overridden method is in timestamp.

3.2 Date, Timestamp

The parent class cannot be converted directly to the subclass, with the help of the intermediate string~~~~

Java.sql.Date only store date data does not store time data
Time data is lost
Preparedstatement.setdate (1, New Java.sql.Date (Date.gettime ()));
You can handle this.
Preparedstatement.settimestamp (1, New Java.sql.Timestamp (New Java.util.Date (). GetTime ()));

To get the complete data, including the date and time, you can
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 ());
If you fill it out
In this case:
You can receive the Java.util.Date type when you store the database 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 the Long value.
From the storage database, you can get timestamp with his gettime () method to get a long value, and then the long value to construct a Java.util.Date object, so that the date object can be manipulated. Better say new Simpletimeformat ("Yyyyy-mm-dd HH:mm:ss"). Format (), etc.

Conversion between java:string and date, timestamp

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.