// 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. // This version fixes the Zune bug. // today: System.currentTimeMillis() // Dec 30, 2008: 1230624000000l // Dec 31, 2008: 1230710400000l // Jan 1, 2009: 1230850800000l // Jan 1, 2011: 1293922800000l // Dec 31, 2011: 1325372400000l public class Days { public static final int ORIGIN_YEAR = 1970; public static void main(String[] args) { long millis = 1325372400000l; // 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; // in leap years, day 366 is in the current year so stop the loop // in non-leap-years, 366 is NOT in the current year, so go to next year while (day > 366 || (day == 366 && !isLeap(year))) { if (isLeap(year)) { day = day - 366; year++; } else { day = day - 365; year++; } } reportTimes(millis, days, year, day); } public static void reportTimes(long millis, double days, int year, int day) { 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; } }