Java Time Format Conversion tool class

Source: Internet
Author: User

Change the current time to a specified time

 // Change the current time to a specified time      Publicstring Dateutil (Integer seconds, String dateformatpattern) {Date Date=NewDate (); Long Millis= Date.gettime () + seconds * 1000;//Time Millisecond ValueSimpleDateFormat SimpleDateFormat =NewSimpleDateFormat (Dateformatpattern); String Time=Simpledateformat.format (Millis); returnTime ; }
@Test Public voiddatetest () {String firstactualdate= Dateutil ( -1200, "Yyyy-mm-dd hh:mm");//First actual arrival time pointString secondactualdate = dateutil (0, "Yyyy-mm-dd hh:mm");//the second actual arrival time point, PS: The first arrival time must be less than the second arrival time pointString plan_starttime = Dateutil (86400, "Yyyy-mm-dd hh:mm");//scheduled start timeString plan_endtime = Dateutil (345600, "Yyyy-mm-dd hh:mm");//Scheduled arrival timeString departuredate = dateutil (0, "yyyy-mm-dd");//search for the scheduled shift positionSystem.out.println (firstactualdate); System.out.println (secondactualdate); System.out.println (Plan_starttime); System.out.println (Plan_endtime); System.out.println (departuredate); }

Result

2018-04-24 18:58
2018-04-24 19:18
2018-04-25 19:18
2018-04-28 19:18
2018-04-24

Convert time to milliseconds

    //Convert the date format to milliseconds     PublicLong Datetomillis (String date) {SimpleDateFormat SimpleDateFormat=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm"); Long Time=NULL; Try{ Time=simpledateformat.parse (date). GetTime (); } Catch(ParseException e) {e.printstacktrace (); }        returnTime ; } @Test Public voiddatetomillistest () {Long date= Datetomillis ("2018-04-25 11:12");    SYSTEM.OUT.PRINTLN (date); }

Result
1524625920000

Java Time Format Conversion tool class

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.