Receive days starting from an era

I want the number of days since the 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 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 output for both. can anyone help where i am wrong.

thank.

+3


source to share


1 answer


The difference between 17006 and 17007 is one day. This difference most likely starts at 7:00 and 23:30 in your time zone on different days in a different time zone, say UTC. Or vice versa, those times in UTC happen on different days in your time zone. Therefore, the counter is disabled by one. I do not know JodaTime, so I cannot give you the exact details.

The difference between 16975 and 17006 is 31 days or a full month. I can tell exactly where it came from. Calendar

months are 0-based: January is month 0, February is 1, etc. This calendar1.set(1970, 1, 1)

sets your calendar to February 1, 1970, 31 days after the epoch. Use instead calendar1.set(1970, Calendar.JANUARY, 1)

. You will also want to control the hours, minutes and seconds Calendar

. You can call clear()

before set()

to make sure the time is at midnight.

If you can use Java 8, you can:

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

      

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 your desired time zone instead ZoneId.systemDefault()

.

+2


source







All Articles