Create a Java Date from a String and vise versa

后端 未结 3 1813
借酒劲吻你
借酒劲吻你 2021-01-28 23:52

I have a date in Integer format(YYYYMMDD). And a start_time as a String (HH:mm 24 hour system)

3条回答
  •  再見小時候
    2021-01-29 00:30

    You have very many representations of date.

    When in doubt, I usually head for getting to unix standard time (milliseconds since 1970) as soon as possible.

    In this case it would be to convert the Integer date to a String, read out the four first as a year, two digits as month and the last two as day day, and then do the similar thing for the 24h time, and create a java.util.Date from this like so:

    SimpleDateFormat dateParser=new SimpleDateFormat("yyyyMMdd HH:mm"); //please double check the syntax for this guy...
    String yyyyMmDd = date.toString();
    String fullDate = yyyyMmDd + " " + start_time;
    java.util.Date startDate = dateParser.parse(fullDate);
    long startTimeInMillis = startDate.getTime();
    final long MILLISECONDS_PER_HOUR = 1000*60*60;
    long durationInMillis = (long)duration*MILLISECONDS_PER_HOUR;
    java.util.Date endDate = new java.util.Date(startTimeInMillis + durationInMillis);
    

    Don't miss Joda time or Java 8 new, finally improved date handling named java.time.

提交回复
热议问题