Conversion between date and string in Java

Source: Internet
Author: User

1: General Ideas

[HTML]View Plaincopy
  1. This conversion is going to use the Java.text.SimpleDateFormat class
  2. The string is converted into a date type:
  3. Method 1:
  4. The simplest method is the date date=new Date ("2008-04-14");
  5. Method 2:
  6. SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");//lowercase MM indicates minutes
  7. String dstr="2008-4-24";
  8. Java.util.Date date=sdf.parse (DSTR);
  9. The date is converted into a string:
  10. SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
  11. Java.util.Date date=new Java.util.Date ();
  12. String str=sdf.format (date);

2: program Instance

[HTML]View Plaincopy
  1. Package test;
  2. Import Java.text.DateFormat;
  3. Import Java.text.SimpleDateFormat;
  4. Import java.text.ParseException;
  5. Import Java.util.Date;
  6. public class Stringordate {
  7. public static string datetostring (date date, String type) {
  8. String str = null;
  9. DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
  10. if (Type.equals ("short")) {
  11. 07-1-18
  12. format = dateformat.getdateinstance (dateformat.short);
  13. str = Format.format (date);
  14. } else if (Type.equals ("MEDIUM")) {
  15. 2007-1-18
  16. format = dateformat.getdateinstance (dateformat.medium);
  17. str = Format.format (date);
  18. } else if (Type.equals ("full")) {
  19. Thursday, January 18, 2007
  20. format = dateformat.getdateinstance (dateformat.full);
  21. str = Format.format (date);
  22. }
  23. return str;
  24. }
  25. public static Date stringtodate (String str) {
  26. DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
  27. Date date = null;
  28. try {
  29. Fri Feb 00:00:00 CST 2012
  30. date = format.parse (str);
  31. } catch (ParseException e) {
  32. E.printstacktrace ();
  33. }
  34. 2012-02-24
  35. date = java.sql.Date.valueOf (str);
  36. return date;
  37. }
  38. public static void Main (string[] args) {
  39. Date date = new Date ();
  40. System.out.println (stringordate.datetostring (date, "MEDIUM"));
  41. String str = "2012-2-24";
  42. SYSTEM.OUT.PRINTLN (Stringordate.stringtodate (str));
  43. }

Conversion between date and string in Java

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.