Java is shaped like yyyy-mm-dd ' T ' HH:mm:ss. The problem of SSSZ into various formats

Source: Internet
Author: User
Tags date1

Yyyy-mm-dd ' T ' HH:mm:ss. Sssz

The following three SSS refer to milliseconds, Z represents the time zone, and the middle T represents any replaceable character.

Here's a look at the example:
Example One:

@Test public
    void Testtime () throws parseexception{
       Date date = new Date (); 
       SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss. Sssz ");
       SimpleDateFormat df1 = new SimpleDateFormat ("Yyyy-mm-dd ' Test" HH:mm:ss.) Sssz "); 
       String str = df.format (date);
       String str1 = Df1.format (date);     
       System.out.println (str);
       System.out.println (STR1);     
    }

Test results

2017-06-08t10:41:06.261+0800
2017-06-08 Test 10:41:06.261+0800

Example Two:

Convert 2017-05-18t10:26:10.488z to YYYY-MM-DD HH:mm:ss or YYYYMMDDHHMMSS format

@Test public
    void TestTime1 () throws parseexception{
       String datestr = "2017-05-18t10:26:10.488z";
       SimpleDateFormat DFF = new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss.") SSS ", Locale.english)//converted time format of input
       SimpleDateFormat df1 = new SimpleDateFormat (" Yyyy-mm-dd HH:mm:ss "); The time format needed to be converted
       SimpleDateFormat df2 = new SimpleDateFormat ("Yyyymmddhhmmss");
       Date date1 = Dff.parse (DATESTR);     
       String str1 = Df1.format (date1);
       String str2 = Df2.format (date1);
       System.out.println ("str1 is" +str1);
       System.out.println ("str2 is" +str2);
    }

Run Result:

STR1 is 2017-05-18 10:26:10
str2 is 20170518102610

All of the above are Pro test: Fair trade.

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.