// This program determines what year it is right now and what day of the year // it is. 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 has a bug. It goes into an infinite loop on the 366th day // of a leap year. This happened to Zune players on 12/31/08 because they // had similar code. public class Days1 { public static void main(String[] args) { long millis = System.currentTimeMillis(); // we subtract 1/3 to account for Pacific Time being 8 hours // behind Greenwich Mean Time; double days = toDays(millis) - 1.0 / 3.0; System.out.println("millis = " + millis); System.out.println("days = " + days); // we add 1 because 1/1/70 is day 1 of 1970, not day 0 int day = (int) days + 1; // the zune bug will show up if you include this line of code: // day = 14245; int year = 1970; while (day > 365) { if (year % 4 == 0) { if (day > 366) { day = day - 366; year++; } } else { day = day - 365; year++; } } System.out.println("year = " + year); System.out.println("day = " + day); } // converts milliseconds to days public static double toDays(long millis) { return millis / 1000.0 / 60.0 / 60.0 / 24.0; } }