简体   繁体   English

自纪元以来的时间和日期

[英]Time and Date since epoch

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.我所能使用的只是System.currentTimeMillis()来获取自纪元以来的当前时间(以毫秒为单位),并java.util.TimeZone.getDefault().getRawOffset()来说明我自纪元以来的时区。

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.到目前为止,我所尝试的是以毫秒为单位的当前时间除以 1000 得到秒,然后 60 得到分钟,然后 60 得到小时,依此类推。 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.这工作正常,直到我从 1970 年 1 月 1 日开始算起 18184 天。我不能简单地将这些天完全除以 30 或 31,因为并非所有月份都有 30 或 31 天。 i can't find years either because of leap years being 365 or 364 days in them.我也找不到年份,因为闰年是 365 或 364 天。

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 .我需要结束代码来简单地打印10/15/2019October 15, 2019 the format doesnt matter i just need it to print the current day, month, and year based on the epoch milliseconds格式无关紧要,我只需要它根据纪元毫秒打印当前日期、月份和年份

Here's a version for you.这是给你的一个版本。 This uses 4-year blocks with a constant number of days, which works up until year 2100.这使用具有恒定天数的 4 年块,直到 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM