How to calculate time difference in java?

后端 未结 17 1387
醉酒成梦
醉酒成梦 2020-11-22 16:33

I want to subtract two timeperiods say 16:00:00 from 19:00:00. Is there any java function for this? The results can be in milliseconds, seconds, or minutes.

相关标签:
17条回答
  • 2020-11-22 16:50
    String time1 = "16:00:00";
    String time2 = "19:00:00";
    
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    Date date1 = format.parse(time1);
    Date date2 = format.parse(time2);
    long difference = date2.getTime() - date1.getTime(); 
    

    Difference is in milliseconds.

    I modified sfaizs post.

    0 讨论(0)
  • 2020-11-22 16:51

    TO get pretty timing differences, then

    // d1, d2 are dates
    long diff = d2.getTime() - d1.getTime();
    
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);
    
    System.out.print(diffDays + " days, ");
    System.out.print(diffHours + " hours, ");
    System.out.print(diffMinutes + " minutes, ");
    System.out.print(diffSeconds + " seconds.");
    
    0 讨论(0)
  • 2020-11-22 16:51

    Аlternative option if time from different days is taken, for example: 22:00 and 01:55.

    public static long getDiffTime(Date date1, Date date2){
            if (date2.getTime() - date1.getTime() < 0) {// if for example date1 = 22:00, date2 = 01:55.
                Calendar c = Calendar.getInstance();
                c.setTime(date2);
                c.add(Calendar.DATE, 1);
                date2 = c.getTime();
            } //else for example date1 = 01:55, date2 = 03:55.
            long ms = date2.getTime() - date1.getTime();
    
            //235 minutes ~ 4 hours for (22:00 -- 01:55).
            //120 minutes ~ 2 hours for (01:55 -- 03:55).
            return TimeUnit.MINUTES.convert(ms, TimeUnit.MILLISECONDS);
        }
    
    0 讨论(0)
  • 2020-11-22 16:53
    import java.util.Date;
    ...
    Date d1 = new Date();
    ...
    ...
    Date d2 = new Date();
    System.out.println(d2.getTime()-d1.getTime()); //gives the time difference in milliseconds. 
    System.out.println((d2.getTime()-d1.getTime())/1000); //gives the time difference in seconds.
    

    and, to show in a nicer format, you can use:

        DecimalFormat myDecimalFormatter = new DecimalFormat("###,###.###");
        System.out.println(myDecimalFormatter.format(((double)d2.getTime()-d1.getTime())/1000));
    
    0 讨论(0)
  • 2020-11-22 16:55

    Just like any other language; convert your time periods to a unix timestamp (ie, seconds since the Unix epoch) and then simply subtract. Then, the resulting seconds should be used as a new unix timestamp and read formatted in whatever format you want.

    Ah, give the above poster (genesiss) his due credit, code's always handy ;) Though, you now have an explanation as well :)

    0 讨论(0)
  • 2020-11-22 16:56

    Java 8

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    LocalDateTime dateTime1= LocalDateTime.parse("2014-11-25 19:00:00", formatter);
    LocalDateTime dateTime2= LocalDateTime.parse("2014-11-25 16:00:00", formatter);
    
    long diffInMilli = java.time.Duration.between(dateTime1, dateTime2).toMillis();
    long diffInSeconds = java.time.Duration.between(dateTime1, dateTime2).getSeconds();
    long diffInMinutes = java.time.Duration.between(dateTime1, dateTime2).toMinutes();
    
    0 讨论(0)
提交回复
热议问题