Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ERROR? parse SimpleDateFormat

Status
Not open for further replies.

FilipVolvo

Programmer
Jan 30, 2001
2
SE
Can anyone explain this 'strange' result?

When I parse a date with the pattern yyyy-ww-E
I get a bad result!!!

pattern: yyyy-ww-E
parse : 2002-48-Sun -> in calendar
format : calendar -> 2002-47-Sun

WHY ?????


The Code:
----------

import java.util.*;
import java.text.*;

public class Test3 {

public static void main(String[] args) {
GregorianCalendar c = new java.util.GregorianCalendar();

c.setFirstDayOfWeek(c.MONDAY);

SimpleDateFormat sdf = new SimpleDateFormat();

sdf.setCalendar(c);

sdf.applyLocalizedPattern("yyyy-ww-E");

try {
c.setTime(sdf.parse("2002-48-Sun"));
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(sdf.format(c.getTime()));
}
}


With regards
Filip
 
It seems to be struggling to reconcile the data you're giving it. I always consider that somewhere in the parse routine, it must be constructing either a Date object or a Calendar object, starting with default values (date and time right now). Going left to right, it would then reset the year (easy enough), then set the week of the year ... what day of the week would it default to at that point? Probably the first day of the week, which you've defined as Monday. Then the next thing that gets set is the day of the week (Sunday). It already has the week of the year set, and the nearest Sunday is in the previous week, so it rolls back the week of the year.

If you eliminate setting the first day of the week to Monday, it works fine.

It's not that the result is bad, it just isn't what you're expecting...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top