Java Time Conversion

Source: Internet
Author: User

1 Importjava.text.ParseException;2 ImportJava.text.SimpleDateFormat;3 ImportJava.util.Calendar;4 Importjava.util.Date;5 6 /**7 * Created by Edward on 2016/6/30.8  */9  Public classTimeutil {Ten  One  A     /** - * Convert 1467341232351 to the specified format Yyyy-mm-dd HH:mm:ss. SSS " -      * @paramTime "1467342217645" the      * @parampattern "Yyyy-mm-dd HH:mm:ss. SSS " -      * @return2016-07-01 11:03:37.645 -      */ -  +      Public Staticstring parselongtime2string (string time, String pattern) { -Calendar Calendar =calendar.getinstance (); + Calendar.settimeinmillis (long.valueof (time)); A         return NewSimpleDateFormat (pattern). Format (Calendar.gettime ()); at     } -  -  -     /** - * Converts a string of the specified format to a long -      * @paramTime "2016-07-01 10:52:41.792" in      * @parampattern "Yyyy-mm-dd HH:mm:ss. SSS " -      * @return1467341561792 to      */ +      Public Static LongParsestring2longtime (string time, string pattern) -     { theDate Date =NULL; *         Try { $Date =NewSimpleDateFormat (pattern). Parse (time);Panax Notoginseng}Catch(ParseException e) { - e.printstacktrace (); the         } +         returndate.gettime (); A     } the  +      Public Static voidMain (string[] args) -     { $Calendar Calendar =calendar.getinstance (); $  - System.out.println (Calendar.gettimeinmillis ()); -  theSystem.out.println (Timeutil.parselongtime2string (string.valueof (Calendar.gettimeinmillis ()), "Yyyy-mm-dd HH:mm: Ss. SSS ")); - WuyiSystem.out.println (Timeutil.parsestring2longtime ("2016-07-01 10:52:41.792", "Yyyy-mm-dd HH:mm:ss. SSS ")); the  -     } Wu  -}

Java Time Conversion

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.