Conversion between java:string and date, timestamp

Source: Internet
Author: User
Tags dateformat

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

1.1 String--Date

Java code
  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

Java code
  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

Java code
  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 yyyy-mm-dd hh:mm:ss[.f...] 这样的格式,中括号表示可选,否则报错!!! shape such as:

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

Java code
  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

Java code
    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~~~~

Note: Use the following method to be more concise

Timestamp ts = new Timestamp (Date.gettime ());

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.