Parsing a String into Date with DateFormat not parsing correctly

后端 未结 1 1418
梦如初夏
梦如初夏 2020-12-12 01:15

I\'ve been searching all over and just can\'t find a explanation or reason why this is happening but the parse(String) method of DateFormat just isn\'t parsing my String cor

相关标签:
1条回答
  • 2020-12-12 01:44

    The result format is the default format of Date#toString() (click link to see the javadoc). You're apparently doing a System.out.println(date). You would like to use SimpleDateFormat#format() instead with another pattern to format it in the desired format. E.g.

    String newDateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    

    Update: You shouldn't care about the format when using Date. You should only care about the format at that point when Date is to be converted (displayed) as String. As to the difference in timestamps, the Date uses millisecond precision for the timestamp while HTTP header uses second precision. You'd like to divide the timestamps by 1000 before comparing.

    0 讨论(0)
提交回复
热议问题