2

I am trying to write a program that simply prints the current date. so all i need is year, month, and day. I am not allowed to use any date, calendar, gregorian calendar, or time classes in order to obtain the current date. All i am able to use is System.currentTimeMillis() to get the current time in milli seconds since epoch and java.util.TimeZone.getDefault().getRawOffset() to account for my time zone since epoch.

What i have tried so far is taking the current time in milliseconds and dividing by 1000 to get seconds and then 60 to get minutes and then 60 to get hours and so on. Which works fine until i get down to days which is 18184 since epoch january 1, 1970. I cant simply divide the days completely by 30 or 31 because not all months have 30 or 31 days. i can't find years either because of leap years being 365 or 364 days in them.

class JulianDate {
    static void main() {
        long currentMilli = System.currentTimeMillis() + java.util.TimeZone.getDefault().getRawOffset();
        long seconds = currentMilli / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        System.out.println("Days since epoch : "  + days);
    }
}

I need the end code to simply print 10/15/2019 or October 15, 2019. the format doesnt matter i just need it to print the current day, month, and year based on the epoch milliseconds

Miedena
  • 123
  • 1
  • 12
Gamand
  • 21
  • 2
  • Do you think maybe you might be doing it backwards? You can google for a formula to determine leap years. Then count years after 1 Jan 1970 in 365 or 366 chunks, as appropriate. Then when your remaining number of days is less than one year, count off months by the actual number of days in the month. You're always working from Jan 1, so you know what month you're dealing with, as well as whether it's a leap year. Then when you have fewer than 30 days and it's not Feb, start counting off individual days. – MarsAtomic Oct 16 '19 at 03:36
  • You might be interested to read this thread: https://stackoverflow.com/questions/1021324/java-code-for-calculating-leap-year – Mark Bramnik Oct 16 '19 at 04:21

1 Answers1

0

Here's a version for you. This uses 4-year blocks with a constant number of days, which works up until year 2100.

public static void main(String[] args) {
    // Simple leap year counts, works until 2100
    int[] daysIn4Years = { 365, 365, 366, 365 }; // Starting 1970, 1971, 1972, 1973
    int daysIn4YearsTotal = IntStream.of(daysIn4Years).sum();
    int[][] daysInMonths = {
        { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }};
    int epochYear = 1970;
    long msIn1Day = 24 * 60 * 60 * 1000L;
    TimeZone timeZone = TimeZone.getDefault();

    long msSinceEpoch = System.currentTimeMillis() + timeZone.getRawOffset();
    int day = (int) (msSinceEpoch / msIn1Day);

    // Get the 4-year block
    int year = ((day / (daysIn4YearsTotal)) * 4) + epochYear;
    day %= daysIn4YearsTotal;

    // Adjust year within the block
    for (int daysInYear : daysIn4Years) {
        if (daysInYear > day) break;
        day -= daysInYear;
        year++;
    }
    boolean leapYear = year % 4 == 0; // simple leap year check < 2100

    // Iterate months
    int month = 0;
    for (int daysInMonth : daysInMonths[leapYear ? 1 : 0]) {
        if (daysInMonth > day) break;
        day -= daysInMonth;
        month++;
    }

    // day is 0..30, month is 0..11
    System.out.printf("%d/%d/%d", month + 1, day + 1, year);
}
boot-and-bonnet
  • 731
  • 2
  • 5
  • 16