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