Calendarweek in SimpleDateFormat

Discussion in 'Java' started by Peter Rank, Jan 25, 2008.

  1. Peter Rank

    Peter Rank Guest

    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
     
    Peter Rank, Jan 25, 2008
    #1
    1. Advertising

  2. Peter Rank

    Peter Rank Guest

    Peter Rank, Jan 25, 2008
    #2
    1. Advertising

  3. In comp.lang.java.programmer message <59ccbe7a-6b50-4ef1-b9d1-5d95fa8b29
    >, Fri, 25 Jan 2008 10:44:43, Peter Rank
    <> posted:
    >
    >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.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Jan 26, 2008
    #3
  4. Peter Rank

    Arne Vajhøj Guest

    Peter Rank wrote:
    > 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
     
    Arne Vajhøj, Jan 26, 2008
    #4
  5. Peter Rank

    Arne Vajhøj Guest

    Arne Vajhøj, Jan 26, 2008
    #5
  6. In comp.lang.java.programmer message <479ba463$0$90272$
    site.dk>, Sat, 26 Jan 2008 16:21:42, Arne Vajhøj <>
    posted:
    >Peter Rank wrote:
    >> 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.


    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.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
     
    Dr J R Stockton, Jan 27, 2008
    #6
  7. Peter Rank

    Arne Vajhøj Guest

    Dr J R Stockton wrote:
    > In comp.lang.java.programmer message <479ba463$0$90272$
    > site.dk>, Sat, 26 Jan 2008 16:21:42, Arne Vajhøj <>
    > posted:
    >> Peter Rank wrote:
    >>> 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.

    >
    > 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
     
    Arne Vajhøj, Jan 27, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aaapaul
    Replies:
    0
    Views:
    425
    aaapaul
    Dec 17, 2003
  2. Manuel
    Replies:
    0
    Views:
    572
    Manuel
    Aug 28, 2003
  3. Niko
    Replies:
    10
    Views:
    2,517
    Wojtek
    Sep 18, 2003
  4. Eric

    SimpleDateFormat

    Eric, Sep 24, 2003, in forum: Java
    Replies:
    4
    Views:
    9,597
    Chris Smith
    Sep 25, 2003
  5. Bill Angel
    Replies:
    4
    Views:
    1,409
    Bill Angel
    Sep 28, 2003
Loading...

Share This Page