Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
Created December 29, 2014 17:30
Show Gist options
  • Save gigamonkey/75420e4e7c1dc0cc1849 to your computer and use it in GitHub Desktop.
Save gigamonkey/75420e4e7c1dc0cc1849 to your computer and use it in GitHub Desktop.
YYYY vs yyyy
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.GregorianCalendar;
public class Foo {
public static void main(String[] argv) {
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat cy = new SimpleDateFormat("yyyy");
cy.setTimeZone(utc);
SimpleDateFormat wy = new SimpleDateFormat("YYYY");
wy.setTimeZone(utc);
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(utc);
calendar.set(2010, 0, 1);
Date d = calendar.getTime();
String cal = cy.format(d);
String week = wy.format(d);
System.out.println(d + " Calendar year: " + cal + "; Week year: " + week);
}
}
@gigamonkey
Copy link
Author

$ java Foo
Fri Jan 01 01:28:57 PST 2010 Calendar year: 2010; Week year: 2010

@NelsonMinar
Copy link

FWIW, POSIX date doesn't seem to have that problem. At least, on an ubuntu 14.04 system running in UTC timezone.

$ date -d '2010-01-01' -u +'%G-W%V-%u'
2009-W53-5

@gigamonkey
Copy link
Author

Also apparently fixed in newer versions of Java 7 than I have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment