我想在毫秒之间得到两次时间差

时间:2022-01-19 16:54:51

I want to get time difference between two times in milliseconds.

我希望在两毫秒之间得到时间差。

Note

If the difference is above half a second they return 1000 millisecond, so how to get proper millisecond like if difference of half second to get 500 millisecond

如果差异超过半秒,它们返回1000毫秒,那么如何获得适当的毫秒,就好像半秒的差异得到500毫秒

Difference between two times is half second then my code returns 1000 milliseconds that means 1 Second but actually its 0.5 Second so how get 500 milliseconds if difference is half second

两次之间的差异是半秒然后我的代码返回1000毫秒,这意味着1秒但实际上是0.5秒,那么如果差异为半秒则如何获得500毫秒

    Date Date1 = sdf.parse(lastTime);
    Date Date2 = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
    long millie = Date2.getTime() - Date1.getTime();

4 个解决方案

#1


2  

SimpleDateFormat does not include milliseconds by default. So

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

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

does not output the milliseconds and everything is rounded to seconds.

不输出毫秒,一切都舍入到秒。

Define a custom date format including milliseconds like this:

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

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ\"");

#2


1  

If you use java8 then try with this:

如果您使用java8,请尝试使用:

LocalDateTime startTime = LocalDateTime.of(2018,07,31,12,1);
long diff = ChronoUnit.MILLIS.between(startTime,LocalDateTime.now());

#3


0  

you can find difference between two date in millisecond,second,minutes and hours by calling following method, you can change method by your requirements.

您可以通过调用以下方法找到两个日期之间的差异,以毫秒,秒,分钟和小时为单位,您可以根据您的要求更改方法。

    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  

try my below code:

试试下面的代码:

         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;

here i am converting current time from server time . here u have to take difference between two times. i hope you understand above code.let me know is it ok or not... Thanks.

我在这里从服务器时间转换当前时间。在这里,你必须在两次之间取得差异。我希望你能理解上面的代码。我知道是不是没有...谢谢。

#1


2  

SimpleDateFormat does not include milliseconds by default. So

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

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

does not output the milliseconds and everything is rounded to seconds.

不输出毫秒,一切都舍入到秒。

Define a custom date format including milliseconds like this:

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

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ\"");

#2


1  

If you use java8 then try with this:

如果您使用java8,请尝试使用:

LocalDateTime startTime = LocalDateTime.of(2018,07,31,12,1);
long diff = ChronoUnit.MILLIS.between(startTime,LocalDateTime.now());

#3


0  

you can find difference between two date in millisecond,second,minutes and hours by calling following method, you can change method by your requirements.

您可以通过调用以下方法找到两个日期之间的差异,以毫秒,秒,分钟和小时为单位,您可以根据您的要求更改方法。

    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  

try my below code:

试试下面的代码:

         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;

here i am converting current time from server time . here u have to take difference between two times. i hope you understand above code.let me know is it ok or not... Thanks.

我在这里从服务器时间转换当前时间。在这里,你必须在两次之间取得差异。我希望你能理解上面的代码。我知道是不是没有...谢谢。