Skip to content

Instantly share code, notes, and snippets.

@claraj
Last active April 26, 2017 23:14
Show Gist options
  • Save claraj/bc1b73905ee23e0cd34953ce23068c02 to your computer and use it in GitHub Desktop.
Save claraj/bc1b73905ee23e0cd34953ce23068c02 to your computer and use it in GitHub Desktop.
package com.company;
import org.joda.time.DateTime;
import org.joda.time.Days;
import java.util.Date;
/**
* Created by admin on 4/26/17.
*/
public class Dates {
public static void main(String[] args) {
Date dateConsigned = new Date(1488499200000l);
System.out.println(dateConsigned);
Date now = new Date();
// Math method - how many Milliseconds between dates?
long delta = now.getTime() - dateConsigned.getTime();
long msInDay = 1000 * 60 * 60 * 24;
long days = delta / msInDay;
// This ignores any partial days.
System.out.println("The number of days difference is " + days);
// Use Joda-time object
// Joda-time is useful if you end up doing a lot of date and time manipulations.
// Add Joda-time through Maven https://mvnrepository.com/artifact/joda-time/joda-time/2.9.9
// Joda-time counts partial days when computing the difference.
DateTime start = new DateTime(dateConsigned);
DateTime end = new DateTime(now);
int daysDifference = Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays();
System.out.println(daysDifference);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment