从纪元开始的日子

时间:2017-03-28 09:22:17

标签: java datetime jodatime

我想要从纪元(1970-01-01)开始的天数。我试过joda-time

try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date1 = sdf.parse("2013-05-03 07:00:00");
    Date date2 = sdf.parse("2013-05-03 23:30:00");


    MutableDateTime epoch = new MutableDateTime();
    epoch.setDate(0); //Set to Epoch time
    System.out.println("Epoch: " + epoch);
    Days days1 = Days.daysBetween(epoch, new MutableDateTime(date1.getTime()));
    Days days2 = Days.daysBetween(epoch, new MutableDateTime(date2.getTime()));
    System.out.println("1) Days Since Epoch: " + days1.getDays());
    System.out.println("2) Days Since Epoch: " + days2.getDays());
} catch (ParseException e) {
    e.printStackTrace(); 
}

并使用逻辑:

// Create Calendar instance
    Calendar calendar1 = Calendar.getInstance();
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(date);
    // Set the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

// calendar1.set(calendar1.YEAR,calendar1.MONTH,Calendar.DAY_OF_MONTH);         calendar1.set(1970,1,1);

    /*
     * Use getTimeInMillis() method to get the Calendar's time value in
     * milliseconds. This method returns the current time as UTC
     * milliseconds from the epoch
     */
    long miliSecondForDate1 = calendar1.getTimeInMillis();
    long miliSecondForDate2 = calendar2.getTimeInMillis();

    // Calculate the difference in millisecond between two dates
    long diffInMilis = miliSecondForDate2 - miliSecondForDate1;

    /*
     * Now we have difference between two date in form of millsecond we can
     * easily convert it Minute / Hour / Days by dividing the difference
     * with appropriate value. 1 Second : 1000 milisecond 1 Hour : 60 * 1000
     * millisecond 1 Day : 24 * 60 * 1000 milisecond
     */

    long diffInSecond = diffInMilis / 1000;
    long diffInMinute = diffInMilis / (60 * 1000);
    long diffInHour = diffInMilis / (60 * 60 * 1000);
    long diffInDays = diffInMilis / (24 * 60 * 60 * 1000);
    if(logger.isInfoEnabled()) {
        logger.info("Difference in Seconds : " + diffInSecond);
        logger.info("Difference in Minute : " + diffInMinute);
        logger.info("Difference in Hours : " + diffInHour);
        logger.info("Difference in Days : " + diffInDays);
    }

我正在为这两者获得差异化结果。有人可以帮助我错了。

感谢。

1 个答案:

答案 0 :(得分:2)

17006和17007之间的差异是一天。这种差异非常可能来自您所在时区的7:00和23:30,在某些其他时区(例如UTC)的不同日期。或者反过来说,UTC中的那些时间碰巧发生在您所在时区的不同日期。因此,计数是一个。我不知道JodaTime,所以我不能给你准确的细节。

16975和17006之间的差异是31天或整整一个月。我可以告诉你它到底来自哪里。 calendar1.set(1970, 1, 1)个月基于0:1月是0月,2月是1等。因此,calendar1.set(1970, Calendar.JANUARY, 1)将您的日历设置为 2月 1,1970年,即纪元后31天。请改用Calendar。您还需要控制clear()的小时,分​​钟和秒。您可以在set()之前致电 DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); OffsetDateTime t1 = LocalDateTime.parse("2013-05-03 07:00:00", format) .atOffset(ZoneOffset.UTC); OffsetDateTime t2 = LocalDateTime.parse("2013-05-03 23:30:00", format) .atOffset(ZoneOffset.UTC); System.out.println("1) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t1)); System.out.println("2) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t2)); ,以确保时间是午夜。

如果您可以使用Java 8,则可以执行以下操作:

1) Days Since Epoch: 15828
2) Days Since Epoch: 15828

打印:

    DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    ZonedDateTime t1 = LocalDateTime.parse("2013-05-03 07:00:00", format)
            .atZone(ZoneId.systemDefault());
    ZonedDateTime t2 = LocalDateTime.parse("2013-05-03 23:30:00", format)
            .atZone(ZoneId.systemDefault());

    System.out.println("1) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t1));
    System.out.println("2) Days Since Epoch: " + ChronoUnit.DAYS.between(Instant.EPOCH, t2));

如果您想使用其他时区:

ZoneId.systemDefault()

您可以填写所需的时区而不是FirebaseInstanceId.getInstance().deleteInstanceId();