Java SimpleDateFormat error with kk hour formatting

My understanding is that kk should call hours in the range 1-24. However, there seems to be a problem with how the days change in this formatting.

Here is some sample code and its output to illustrate my point:

Long HOUR = 3600000L;

SimpleDateFormat kkFormat = new SimpleDateFormat("yyyy-MM-dd kk");
SimpleDateFormat HHFormat = new SimpleDateFormat("yyyy-MM-dd HH");

Date date = kkFormat.parse("2015-05-20 21");

for(int i=0; i<5; i++){
    System.out.println(kkFormat.format(new Date(date.getTime() + i * HOUR)));
    System.out.println(HHFormat.format(new Date(date.getTime() + i * HOUR)));
    System.out.println();
}

      

This generates the following output:

2015-05-20 21
2015-05-20 21

2015-05-20 22
2015-05-20 22

2015-05-20 23
2015-05-20 23

2015-05-21 24
2015-05-21 00

2015-05-21 01
2015-05-21 01

      

The problem I see is with "2015-05-21 24" unless that date is formatted as "2015-05-20 24".

Thanks for the clarification.

edit: In response to Dan Getz's question I am trying to create filenames that are repeated like this:

2015052023.txt

2015052024.txt

2015052101.txt

+3


source to share


1 answer


On what day it depends on the time zone, it does not depend on the time the clock is written. So if the date is 21st, then the date is 21st, the end of the story. Writing "12am" as "00" or "24" is irrelevant.

Perhaps you forgot that there are 60 different minutes in an hour? It's not just the first moment the clock hits midnight, it's the full hour after that that we're talking about, which is clearly part of the next 24-hour day.



I personally will never use 24 times in 12 hours because of this ambiguity. Perhaps you could change your question, why would you want to use 24 as a clock for this? Is your data model truly timestamped to the millisecond or similar? Or are you hoping to save and print something else?

+1


source







All Articles