Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get days starting from epoch

I want count of days starting from epoch(1970-01-01). I tried with 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(); 
}

and using logic:

// 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);
    }

I am getting diff result for both of this. can somebody help where i am wrong.

thanks.

like image 839
P Punam Avatar asked Oct 17 '22 15:10

P Punam


1 Answers

The difference between 17006 and 17007 is one day. This difference very likely comes from 7:00 and 23:30 in your time zone are on different days in some other time zone, say, UTC. Or the other way around, those times in UTC happen to be on different days in your time zone. Therefore the count is off by one. I don’t know JodaTime, so I cannot give you the precise details.

The difference between 16975 and 17006 is 31 days or a full month. I can tell you exactly where this comes from. Calendar months are 0-based: January is month 0, February is 1, etc. So calendar1.set(1970, 1, 1) sets your Calendar to February 1, 1970, 31 days after the epoch. Use calendar1.set(1970, Calendar.JANUARY, 1) instead. You will also want to control the hours, minutes and seconds of the Calendar. You may call clear() before set() to make sure the time is at midnight.

If you can use Java 8, you may do:

    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));

This prints:

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

If you want to use a different time zone:

    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));

You can fill in the desired time zone instead of ZoneId.systemDefault().

like image 95
Ole V.V. Avatar answered Oct 21 '22 03:10

Ole V.V.