Date using XXXX-XX-XX XX: XX format in Java

Source: Internet
Author: User

Date date = new Date ();

System. out. println (date. toLocaleString ());

The preceding program uses the preceding format to print the current date.

The following is a date object constructed based on the strings in the preceding format for the program to use.

Public static Date constructDate (String time ){

Boolean result = Pattern. matches ("\ d {4}-\ d {2}-\ d {2} \ d {2 }:\\ d {2 }: \ d {2} ", time );

If (! Result)

Return null;

String ymd = time. split ("") [0];
String hms = time. split ("") [1];
String [] ymds = ymd. split ("-");
String [] hmss = hms. split (":");
Return new Date (Integer. parseInt (ymds [0])-1900, Integer. parseInt (ymds [1])-1, Integer. parseInt (ymds [2]),
Integer. parseInt (hmss [0]), Integer. parseInt (hmss [1]), Integer. parseInt (hmss [2]);
}

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.