Java time conversion and java time Conversion

Source: Internet
Author: User

Java time conversion and java time Conversion

 

1 import java. text. parseException; 2 import java. text. simpleDateFormat; 3 import java. util. calendar; 4 import java. util. date; 5 6/** 7 * Created by Edward on 2016/6/30. 8 */9 public class TimeUtil {10 11 12/*** 13 * converts 1467341232351 to the specified format "yyyy-MM-dd HH: mm: ss. SSS "14 * @ param time" 1467342217645 "15 * @ param pattern" yyyy-MM-dd HH: mm: ss. SSS "16 * @ return 11:03:37. 64517 */18 19 public static String parseLongTime2String (String time, String pattern) {20 Calendar ar calendar = Calendar ar. getInstance (); 21 calendar. setTimeInMillis (Long. valueOf (time); 22 return new SimpleDateFormat (pattern ). format (calendar. getTime (); 23} 24 25 26/** 27 * convert the string in the specified format to long28 * @ param time "10:52:41. 792 "29 * @ param pattern" yyyy-MM-dd HH: mm: ss. SSS "30 * @ return 146734156179231 */32 public static long parseString2LongTime (String time, String pattern) 33 {34 Date date = null; 35 try {36 date = new SimpleDateFormat (pattern ). parse (time); 37} catch (ParseException e) {38 e. printStackTrace (); 39} 40 return date. getTime (); 41} 42 43 public static void main (String [] args) 44 {45 Calendar calendar AR = Calendar ar. getInstance (); 46 47 System. out. println (calendar. getTimeInMillis (); 48 49 System. out. println (TimeUtil. parseLongTime2String (String. valueOf (calendar. getTimeInMillis (), "yyyy-MM-dd HH: mm: ss. SSS "); 50 51 System. out. println (TimeUtil. parseString2LongTime ("2016-07-01 10:52:41. 792 "," yyyy-MM-dd HH: mm: ss. SSS "); 52 53} 54 55}

 

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.