Java Time Conversion Helper class

Source: Internet
Author: User
Tags locale string format

Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.Locale;/*** Created by caohuiming on 2017/10/27.*/ Public classDatetimeutil { Public Static FinalString Datetime_format = "Yyyy-mm-dd HH:mm:ss";  Public Static FinalString date_format = "Yyyy-mm-dd"; //Region provides method area externally     Public StaticDate getDate (String datestring, string format) {string Newformat=format; if(Format = =NULL|| "". Equals (Format.trim ())) {Newformat=Datetime_format; } SimpleDateFormat Formatter=NewSimpleDateFormat (Newformat); Try {            returnFormatter.parse (datestring); } Catch(ParseException e) {return NULL; }    }     Public StaticString Tostr (Final LongTimeStamp,FinalString format) {Calendar cal=calendar (); Cal.settimeinmillis (TimeStamp*1000); Date Date=Cal.gettime (); FinalSimpleDateFormat CustomFormat =NewSimpleDateFormat ();        Customformat.applypattern (Format.trim ()); returnCustomformat.format (date); }    //endregion External provision of method areas//Region Private Method Area    /*** Current Time *@return     */    Private StaticCalendar calendar () {FinalCalendar cal =calendar.getinstance (Locale.chinese);        Cal.setfirstdayofweek (Calendar.monday); returnCal; }    //endregion Private Method Zone}
Time Conversion Helper class
  Public Static voidMain (string[] args)throwsIOException {Longdatetime_2047=2429913600l;//2047-01-01String datetimestr=datetimeutil.tostr (Datetime_2047,datetimeutil.date_format); System.out.println (DATETIMESTR);//2047-01-01Date mydate= Datetimeutil.getdate ("2047-10-27 23:22:21", Datetimeutil.datetime_format); LongMydatelong=mydate.gettime ()/1000; System.out.println (Mydatelong);//2455802541}
test Method

Java Time Conversion Helper class

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.