Conversion between date and string in Java

Source: Internet
Author: User
Tags dateformat

1: General idea   [html] View plaincopy This conversion takes the Java.text.SimpleDateFormat class     string to the date type:   Method 1:     is also the simplest way to date date=new date ("2008-04-14");     Method 2:    simpledateformat sdf=new simpledateformat ("Yyyy-mm-dd");//lowercase MM indicates minutes  string dstr= "2008-4-24";  java.util.date Date=sdf.parse (DSTR);         Date converted to string:    simpledateformat sdf=new SimpleDateFormat (" Yyyy-mm-dd ");  java.util.date date=new java.util.Date ();  string Str=sdf.format (date);   2: program instance [HTML] view plaincopypackage test;  import Java.text.DateFormat;  import Java.text.SimpleDateFormat;  import java.text.ParseException;  import java.util.Date;  public class Stringordate {     public static string datetostring (date date, String type) { &N Bsp       String str = null;          DateFormat format = new simpledAteformat ("Yyyy-mm-dd");          if (type.equals ("short")) {            //07-1-18 & nbsp;            format = dateformat.getdateinstance (Dateformat.short);              str = format.format (date);         } else if (Type.equals ("MEDIUM")) {            //2 007-1-18              format = dateformat.getdateinstance (Dateformat.medium);              str = format.format (date);         } else if (Type.equals ("full")) {            //200 7 January 18 Thu              format = dateformat.getdateinstance (dateformat.full);              str = format.format (date);         }          return str;     }      public static Date stringtodate (String str) {         DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");          Date date = null;          try {            //Fri 00:00:00 CST-&N bsp;            date = Format.parse (str);          } catch (ParseException e) {             E.PRI Ntstacktrace ();         }         //2012-02-24          Date = Java.sql.Date.valueOf (str);                                  &NBS P                     return date;     }      PubLic static void Main (string[] args) {         Date date = new Date ();          System.out.println (stringordate.datetostring (date, "MEDIUM"));          String str = "2012-2-24";          SYSTEM.OUT.PRINTLN (stringordate.stringtodate (str));     }

Conversion between date and string in Java

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.