Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IMPORTANT! Wrong week of year in GregorianCalendar

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a method which return the WEEK_OF_YEAR.

On my local server it works fine, but on the host server it fails after the 25. of December 2004. It seems that week 53 is missing (se documentation below!).

Code:

/*
* @input
* String timestamp (yyyy-mm-dd)
* @output
* int week of year
*/

public int getWeek_In_Year( String timestamp ){

java.util.GregorianCalendar gcl = new java.util.GregorianCalendar( Integer.parseInt( timestamp.substring(0,4) ), Integer.parseInt( timestamp.substring( 5, 7 ) )-1, Integer.parseInt( timestamp.substring( 8, 10 ) ) );

int week = gcl.get( java.util.GregorianCalendar.WEEK_OF_YEAR );

return week;
}

Output documentation:

Data output on the host server:
Browser output: 2005, week 2
Input String: 03-01-2005 (week 1)

Browser output: 2005, week 1
Input String: 01-01-05 (week 53)

Browser output: 2004, week 1
Input String: 31-12-2004 (week 53)
Input String: 27-12-2004 (week 53)
Input String: 26-12-2004 (week 52)

Browser output: 2004, week 52
Input String: 25-12-2004 (week 52)


Can anyone help, please!
Regards Jeppe (Denmark)
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved with:
java.util.GregorianCalendar cal = new java.util.GregorianCalendar( new Locale("da") );
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jeppe:

I see you've been around for about a year here on the Ranch, but as you haven't posted very much, it seems that no one has pointed you to our Naming Policy. While the number one rule on the Ranch is to "Be Nice," the Naming Policy ranks pretty high up there. Please change your display name to comply (We are looking for a first and last name...)

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic