The conversion of date and string formats, the format syntax for various times----------

Source: Internet
Author: User
Tags date1 dateformat string format

Date and string format conversions are very common and are recorded here. facilitate query writing.

Standard writing "yyyy-mm-dd HH:mm:ss", Time is 24-hour system

The following is a simple Java test code:

SimpleDateFormat format1 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); SimpleDateFormat format2 = new SimpleDateFormat ("yyyy year mm DD Day hh when mm minute ss seconds"); Date date1 = null; Date date2 = null; String str1 = "2009-02-14 12:00:00"; String str2 = "February 14, 2009 12:00 00 seconds"; String date:string must be in strict accordance with the defined format try {date1 = Format1.parse (str1); date2 = Format2.parse (str2);} catch (ParseException Ex) {Logger.getlogger (Main.class.getName ()). log (Level.severe, NULL, ex);} System.out.println ("date1=" +date1); System.out.println ("date2=" +date2); Date turns string string str3 = null; String STR4 = null; STR3 = Format1.format (date1); STR4 = Format2.format (date2); System.out.println ("str3=" +STR3); System.out.println ("str4=" +STR4);

Output results:

date1= Sat Feb 12:00:00 CST 2009
date2= Sat Feb 12:00:00 CST 2009
str3= 2009-02-14 12:00:00
str4= February 14, 2009 12:00 00 seconds

To set the date date type for a specific format:

DateFormat Mediumdateformat = dateformat.getdatetimeinstance (Dateformat.medium, Dateformat.medium); System.out.println (Mediumdateformat.format (date)); Output: YYYY-MM-DD hh:mm:ss format Date type such as: 2009-6-24 12:02:28

Date-> String

String = (new SimpleDateFormat ("format")). Format (Date);

String-> Date

Date = (new SimpleDateFormat ("format")). Parse (String);

If it is a date-matched string in a program, it can be stored directly in a date/datetime type of field in the database, and the system will automatically be saved as a date/datetime type. The premise is that the format of the string must be written to OH.

  simpledateformat syntax:   
          G--  Age marker
         y-year
          M-month  

         D--day
          H-hour  12 (1~12)
         H- 24 Hour (0~23)
          m--minutes
         S--SEC
         S--milliseconds
         E--week
         D--Day of the year
          F--the first few weeks of the January
         W--The first few weeks of the year
          W--the first few weeks of January


         a morning/afternoon marker
          K when in a day (1~24)
         K in the morning or afternoon (0~11)
          Z time zone

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.