Calendarweek in SimpleDateFormat

P

Peter Rank

Hi,

I want to display a String that contains the calendarweek and the
year. So I used the following format:

SimpleDateFormat weekFormat = new SimpleDateFormat("'KW' ww '/'
yyyy");
GregorianCalendar gc = new GregorianCalendar(2007,11,31);
System.out.println(weekFormat.format(gc.getTime()));

The result is: KW 01 / 2007

but what I would expect is: KW 53 / 2007 (KW 1 / 2008 would be okay
anyway)

Okay, ist's not quite difficult to code something to get the right
result, but ist there a solution that just uses the SimpleDateFormat-
Class to get the expected result?

Thanks
Peter
 
D

Dr J R Stockton

In comp.lang.java.programmer message <59ccbe7a-6b50-4ef1-b9d1-5d95fa8b29
(e-mail address removed)>, Fri, 25 Jan 2008 10:44:43, Peter Rank
Okay, ist's not quite difficult to code something to get the right
result, but ist there a solution that just uses the SimpleDateFormat-
Class to get the expected result?

With week numbering, one should say which numbering scheme is wanted.
You seem to be German, so you should be using DIN ??? / EN 28601 / ISO
8601. 2007-12-31 was 2008-W01-1; Week 53 of 2007 can only be obtained
under some alien scheme.
 
A

Arne Vajhøj

Peter said:
I want to display a String that contains the calendarweek and the
year. So I used the following format:

SimpleDateFormat weekFormat = new SimpleDateFormat("'KW' ww '/'
yyyy");
GregorianCalendar gc = new GregorianCalendar(2007,11,31);
System.out.println(weekFormat.format(gc.getTime()));

The result is: KW 01 / 2007

but what I would expect is: KW 53 / 2007 (KW 1 / 2008 would be okay
anyway)

Okay, ist's not quite difficult to code something to get the right
result, but ist there a solution that just uses the SimpleDateFormat-
Class to get the expected result?

Technically it is correct.

The week of the date is 1 (of year 2008) and the year of the
date is 2007.

Put together it does not look good.

You will need to work around it.

Arne
 
D

Dr J R Stockton

In comp.lang.java.programmer message <[email protected]
Technically it is correct.

The week of the date is 1 (of year 2008) and the year of the
date is 2007.

That is incorrect : read ISO 8601 (via <URL:http://www.merlyn.demon.co.u
k/datefmts.htm>, if necessary.

new GregorianCalendar(2007,11,31)
is presumably 2007-12-31 in standard Gregorian notation, and it is
2008-W01-1 in standard Week notation.


My Week Calculation page has nothing for Java; but I would add Java
routines if I were convinced that they were correct and efficient and
could be useful. I expect they could easily enough be coded by
reference to the JavaScript algorithms there.
 
A

Arne Vajhøj

Dr said:
In comp.lang.java.programmer message <[email protected]


That is incorrect : read ISO 8601 (via <URL:http://www.merlyn.demon.co.u
k/datefmts.htm>, if necessary.

new GregorianCalendar(2007,11,31)
is presumably 2007-12-31 in standard Gregorian notation, and it is
2008-W01-1 in standard Week notation.

Try read what people write before you accuse them of being incorrect.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top