File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DateFormat" Watch "DateFormat" New topic
Author

DateFormat

tp knotts
Greenhorn

Joined: Feb 01, 2010
Posts: 1
Help please!
According to ISO, http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm, Jan 26th should be week 4 of 2010 for DateFormat ww.
However, when I run the following, I get week 5:

String myStrDate = "01/26/2010";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date myDate = df.parse(myStrDate);
String weekday = new SimpleDateFormat("EEE").format(myDate);
String ww = new SimpleDateFormat("ww").format(myDate);
System.out.print(myDate + "\n" + weekday + "\n" + ww);
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Number of week in a year depends on your locale.
Run this code:

and you'll get week 4 or 5 for different locales:

Tue Jan 26 00:00:00 CET 2010 # 05 # ja_JP
Tue Jan 26 00:00:00 CET 2010 # 05 # es_PE
Tue Jan 26 00:00:00 CET 2010 # 05 # en
Mon Jan 26 00:00:00 CET 3998 # 05 # ja_JP_JP
Tue Jan 26 00:00:00 CET 2010 # 05 # es_PA
Tue Jan 26 00:00:00 CET 2010 # 05 # sr_BA
Tue Jan 26 00:00:00 CET 2010 # 05 # mk
..................................
...................................
Tue Jan 26 00:00:00 CET 2010 # 04 # el
Tue Jan 26 00:00:00 CET 2010 # 05 # ms
Tue Jan 26 00:00:00 CET 2010 # 04 # sv_SE
Tue Jan 26 00:00:00 CET 2010 # 04 # da_DK
Tue Jan 26 00:00:00 CET 2010 # 05 # es_HN
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
the WEEK_OF_YEAR is defined in the API for the GregorianCalendar
Values calculated for the WEEK_OF_YEAR field range from 1 to 53. Week 1 for a year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (as needed).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

The Java classes DateFormat and SimpleDateFormat do not implement the ISO 8601 standard, they have their own rules as Larry quoted. So there's no reason to expect that it will behave the way the ISO 8601 standard specifies.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

and welcome to javaranch tp knotts
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DateFormat