代码之家  ›  专栏  ›  技术社区  ›  Gautam Surani

我想知道两次之间的时间差

  •  -1
  • Gautam Surani  · 技术社区  · 7 年前

    我想得到两次之间的时间差,以毫秒为单位。

    注意

    如果差大于半秒,则返回1000毫秒,因此如何获得 适当的毫秒,比如半秒差得到500毫秒

    两次之间的差是半秒,然后我的代码返回 1000毫秒 那意味着 1秒 但事实上 0.5秒 那么如何得到 五百 毫秒 如果差是半秒

        Date Date1 = sdf.parse(lastTime);
        Date Date2 = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
        long millie = Date2.getTime() - Date1.getTime();
    
    4 回复  |  直到 7 年前
        1
  •  2
  •   Alexander Egger    7 年前

    SimpleDateFormat 默认情况下不包括毫秒。所以

    sdf.format(Calendar.getInstance().getTime());
    

    不输出毫秒,所有内容都四舍五入为秒。

    定义自定义日期格式,包括毫秒,如下所示:

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ\"");
    
        2
  •  1
  •   GolamMazid Sajib    7 年前

    如果你使用 java8 然后试试这个:

    LocalDateTime startTime = LocalDateTime.of(2018,07,31,12,1);
    long diff = ChronoUnit.MILLIS.between(startTime,LocalDateTime.now());
    
        3
  •  0
  •   amit    7 年前

    通过调用下面的方法,您可以找到两个日期(毫秒、秒、分钟和小时)之间的差异,您可以根据需要更改方法。

        public void printDifference(Date startDate) {
    
            Date endDate=new Date();
    
            long different = endDate.getTime() - startDate.getTime(); //Different in miliseconds
    
            System.out.println("startDate : " + startDate);
            System.out.println("endDate : "+ endDate);
            System.out.println("different : " + different);
            int months=0,year=0;
            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;
            long daysInMilli = hoursInMilli * 24;
    
            long elapsedDays = different / daysInMilli;
            different = different % daysInMilli;
    
            long elapsedHours = different / hoursInMilli;
            different = different % hoursInMilli;
    
            long elapsedMinutes = different / minutesInMilli;
            different = different % minutesInMilli;
    
            long elapsedSeconds = different / secondsInMilli;
    
        }
    
        4
  •  0
  •   Revathi Manoharan    7 年前

    请尝试下面的代码:

             Date date = null;
             String value="3 Feb 2018 13:01:41 GMT";
             DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
             formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
             try {
                date = formatter.parse(value);
             } catch (Exception e) {
                e.printStackTrace();
            }
            // Prints the date in the CET timezone
            System.out.println("ConvertTime from serverformat before settimezone ::" + formatter.format(date));
            // Set the formatter to use a different timezone
            formatter.setTimeZone(TimeZone.getDefault());
            // Prints the date in the IST timezone
            System.out.println("ConvertTime from serverformat after settimezone ::" + formatter.format(date));
            DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
            Date dateobj = new Date();
            System.out.println("Current date and time before settimezone:: " + df.format(dateobj));
            df.setTimeZone(TimeZone.getDefault());
            System.out.println("current format after settimezone ::" + df.format(dateobj));
            int difference = 0;
            int diff_Minutes=0;
            int diff_Seconds=0;
            System.out.println("Default values::" + difference+"   "+diff_Minutes+"   "+diff_Seconds);
            difference = (int) (dateobj.getTime() - date.getTime());
            diff_Minutes = difference / (60 * 1000) % 60;
            diff_Seconds = difference / 1000 % 60;
    

    在这里,我将从服务器时间转换为当前时间。在这里,你必须接受两次之间的差异。我希望你能理解上面的代码。让我知道它是否可以。。。 谢谢。