// Code from the Zune music player. // On Dec 31, 2008, all Zunes stopped working! // Explanation: http://www.zuneboards.com/forums/showthread.php?t=38143 // Some dates to test with: // today: System.currentTimeMillis() // Dec 30, 2008: 1230624000000l // Dec 31, 2008: 1230710400000l // Jan 1, 2009: 1230850800000l // Jan 1, 2011: 1293922800000l // Dec 31, 2011: 1325372400000l // Many computer systems keep track of time as number of seconds since Jan 1, 1970. // This program takes a time in this form and determines the corresponding // year and day of the year. For a non-leap year, the day-of-year will be between 1 and 365. // For a leap year, it will be between 1 and 366. public class DaysBad { public static final int ORIGIN_YEAR = 1970; public static void main(String[] args) { long millis = 1230710400000l; // Dec 31, 2008 // we subtract 1/3 to account for Pacific Time being 8 hours // behind Greenwich Mean Time double days = millisToDays(millis) - 1.0 / 3.0; // we add 1 because 1/1/70 is day 1 of 1970, not day 0 int day = (int) days + 1; int year = ORIGIN_YEAR; while (day > 365) { // on the 366th day of a leap year, we get stuck! if (isLeap(year)) { if (day > 366) { // 366 is NOT bigger than 366, fail the test day = day - 366; year++; } } else { day = day - 365; year++; } } System.out.println("millis = " + millis); System.out.println("days = " + days); System.out.println("year = " + year); System.out.println("day = " + day); } // returns whether or not a year is a leap year public static boolean isLeap(int year) { return year % 4 == 0; } // converts milliseconds to days public static double millisToDays(double millis) { return millis / 1000.0 / 60.0 / 60.0 / 24.0; } }