I am developing a drug management program and have recorded the last time I used the drug in my database . I want to have the int of time elapsed since the last consumption , for instance int 6 means is 6 hours ago this drug has used
Asked
Active
Viewed 129 times
-4
-
1get the time in milliseconds and subtract with current time. – Rajnish suryavanshi Oct 07 '19 at 14:18
-
What if the drug was last used 6 hours and 30 minutes ago? How will you store that as an `int` in hour format? – Nexevis Oct 07 '19 at 14:20
-
check this https://stackoverflow.com/questions/3514639/android-java-how-to-subtract-two-times – Anas Mehar Oct 07 '19 at 14:23
-
I took this occasion for writing [a new and modern answer to the linked original question here](https://stackoverflow.com/a/58280871/5772882). – Ole V.V. Oct 08 '19 at 06:30
2 Answers
2
You could take two dates/times and minus them from each other to get a duration. This duration can then be converted into whatever format you need.
The example is an example, your mileage may vary.
LocalDateTime fromDateTime = LocalDateTime.of(2019, 10, 07, 7, 45, 55);
LocalDateTime current = LocalDateTime.now();
long duration = current.toEpochSecond(ZoneOffset.UTC) - fromDateTime.toEpochSecond(ZoneOffset.UTC);
double minutes = duration/60.0;
double hours = duration/3600.0;
System.out.println("Minutes: "+ minutes);
System.out.println("Hours: "+ hours);

Daniel Tung
- 427
- 2
- 14
-
1Using java.time, the modern Java date and time API, is a good idea. I don’t consider `LocalDateTime` the best suited class in this situation, though. Instead of hand coding the calculation I recommend you let a `Duration` object do the work. Or since only whole hours were asked for, `ChronoUnit.HOURS` is another good option. – Ole V.V. Oct 08 '19 at 12:41
1
This will help you:
public class DateFormatter {
public DateFormatter() {
}
public static Date formatDate(String timestamp) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date date = dateFormat.parse(timestamp);
return date;
}
/**
* @param firstDate
* @param secondDate
* @return firstDate - secondDate in days
*/
public static int dateDaysDifference(Date firstDate, Date secondDate) {
Long diff = firstDate.getTime() - secondDate.getTime();
//DecimalFormat decimalFormatter = new DecimalFormat("###,###");
int diffDays = (int) (diff / (24 * 60 * 60 * 1000));
diffDays = Math.abs(diffDays);
return diffDays;
}
/**
* @param firstDate
* @param secondDate
* @return firstDate - secondDate in hours
*/
public static int dateHoursDifference(Date firstDate, Date secondDate) {
Long diff = firstDate.getTime() - secondDate.getTime();
//DecimalFormat decimalFormatter = new DecimalFormat("###,###");
int diffhours = (int) (diff / (60 * 60 * 1000));
//decimalFormatter.format(diffhours);
diffhours = Math.abs(diffhours);
return diffhours;
}
/**
* @param firstDate
* @param secondDate
* @return firstDate - secondDate in minutes
*/
public static int dateMinDifference(Date firstDate, Date secondDate) {
Long diff = firstDate.getTime() - secondDate.getTime();
//DecimalFormat decimalFormatter = new DecimalFormat("###,###");
int diffmin = (int) (diff / (60 * 1000));
//decimalFormatter.format(diffmin);
diffmin = Math.abs(diffmin);
return diffmin;
}
/**
* @param firstDate
* @param secondDate
* @return firstDate - secondDate in seconds
*/
public static int dateSecondDifference(Date firstDate, Date secondDate) {
Long diff = firstDate.getTime() - secondDate.getTime();
//DecimalFormat decimalFormatter = new DecimalFormat("###,###");
int diffsec = (int) (diff / (1000));
//decimalFormatter.format(diffsec);
diffsec = Math.abs(diffsec);
return diffsec;
}
public static void main(String[] args) throws ParseException {
String timestamp2 = "THU JUL 17 00:15:00 CEST 2013";
String timestamp = "FRI JUL 15 11:00:00 CEST 2010";
Date date = formatDate(timestamp);
Date date2 = formatDate(timestamp2);
System.out.println("Difference in days: " + dateDaysDifference(date, date2));
System.out.println("Difference in hours: " + dateHoursDifference(date, date2));
System.out.println("Difference in minutes: " + dateMinDifference(date, date2));
System.out.println("Difference in seconds: " + dateSecondDifference(date, date2));
}
}

Gabriele D'Agostino
- 71
- 3
-
Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 08 '19 at 06:35
-
1