如何相减两次?

时间:2019-10-07 14:17:24

标签: java android database

我正在开发一种药物管理程序,并在数据库中记录了我上次使用该药物的时间。我想知道自上次消费以来已过去的时间,例如int 6的意思是 6小时前该药物已使用

2 个答案:

答案 0 :(得分:1)

您可以将两个日期/时间取为零,并将它们彼此减去以获得持续时间。 然后可以将此持续时间转换为您需要的任何格式。

这个例子是一个例子,您的里程可能会有所不同。

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

答案 1 :(得分:0)

这将帮助您:

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

}