-4

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

Mohamad
  • 117
  • 1
  • 7

2 Answers2

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
  • 1
    Using 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));
}

}

  • 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
    Thank you for suggestion, I'll go study that API. – Gabriele D'Agostino Oct 08 '19 at 06:40