用java计算两次给定时间的b/w的差值

时间:2022-09-28 02:59:07

I am trying to calculate the difference between given two times and i find some problem in it. Suppose we have times value are "11:AM" and 10:00 AM". I am able to calculate but there is a bit of confusion in AM ,PM. Thanks in advance

我试着计算两次给定的差值,我发现其中有一些问题。假设我们有时间值为“11:AM”和“10点AM”。我能计算,但是有一点混乱在上午,PM。谢谢提前

I am using following code:

我使用以下代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

public static String timeDiff(String time1, String time2) {
    String Time = null;
    int difference;
    String a1 = time1.substring(6);
    String a2 = time2.substring(6);

    try {
        // Define a date format the same as the expected input
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm a");
        // Get time values of the date objects
        long l1 = sdf.parse(time1).getTime();
        long l2 = sdf.parse(time2).getTime();
        difference = (int) (l2 - l1);
        difference = (difference < 0 ? -difference : difference) / 60000;
        if (difference > 60) {
            int Hour = difference / 60;
            int Mins = difference % 60;
            if (Mins == 0)
                Time = Hour + " Hours";
            else
                Time = Hour + " Hours" + " " + Mins + " Mins";
        } else {
            Time = difference + " " + "Mins";
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return Time;
}

public static void main(String[] args) {
    String diff = StringToDate.timeDiff("10:00 AM", "01:00 PM");
    System.out.println("Difference: " + diff);
}

}

}

5 个解决方案

#1


6  

You can get date objects for both start & end time. Then get time in millis & subtract.

您可以获得开始和结束时间的日期对象。然后在millis &减去时间。

Difference in Millis = endDate.getTime() - startDate.getTime()

Millis = endDate.getTime() - startDate.getTime()中的差异

Convert the difference to hours/minutes.

把差价转换成小时/分钟。

#2


2  

Since you already have a way to calculate the difference if they are the same AM/PM, you just have to convert them to 24-hour scale:

既然你已经有了计算差异的方法,如果它们是相同的AM/PM,你只需要将它们转换为24小时尺度:

if(time.isPM())
    //add 12 hours to time

and then do your calculations (assuming they are in the same day). This is also assuming you are not just using the Java Calendar class:

然后计算一下(假设它们在同一天)。这还假设您不只是使用Java日历类:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

With Calendar, you can just call

使用日历,你可以直接调用

Calendar.getTimeInMillis()

and subtract them from each other.

然后相减。

#3


1  

For String / Date conversion and / or calculation I recommend you the utilities of Apache Commons: http://commons.apache.org/lang/api-2.6/index.html

对于字符串/日期转换和/或计算,我建议您使用Apache Commons: http://commons.apache.org/lang/api-2.6/index.html

  • StringUtils
  • stringutil
  • DateUtils
  • DateUtils
  • DateFormatUtils
  • DateFormatUtils

Always useful and no need to reinvent the wheel...

总是有用的,不需要重新发明*……

Update: I recently used another library: JodaTime which was very useful for all the date manipulation.

更新:我最近使用了另一个库:JodaTime,它对于所有的日期操作都非常有用。

#4


1  

only the format was incorrect... the line

只是格式不正确……这条线

SimpleDateFormat sdf=new SimpleDateFormat("HH:mm a"); 

should be changed to

应该更改为

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a"); 

everything should be fine after that.

之后一切都会好起来的。

#5


0  

To calculate the difference between two times in 12 hour AM/PM format, try this:

要计算12小时AM/PM格式的两次间隔,请尝试以下方法:

     try {
        SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a"); 

        Date d1 = format.parse("11:59:30 PM");
        Date d2 = format.parse("12:01:00 AM");

        System.out.println("=== Start* :: "+ timeStart);
        System.out.println("=== End*   :: "+ timerEndOrCurrent);

        long difference = d2.getTime() - d1.getTime(); 
        int days = (int) (difference / (1000*60*60*24));  
        int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
        long min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
        hours =  (hours < 0 ? -hours : hours);
        long sec = (int) (difference / 1000) % (60);

        System.out.println("=== hours :: "+ hours);
        System.out.println("=== min   :: "+ ( min < 0 ? ((min-1)+60) : min));
        System.out.println("=== sec   :: "+ ( sec < 0 ? -sec : sec));
        System.out.println("");

    } catch (ParseException e1) {
        e1.printStackTrace();
    }

#1


6  

You can get date objects for both start & end time. Then get time in millis & subtract.

您可以获得开始和结束时间的日期对象。然后在millis &减去时间。

Difference in Millis = endDate.getTime() - startDate.getTime()

Millis = endDate.getTime() - startDate.getTime()中的差异

Convert the difference to hours/minutes.

把差价转换成小时/分钟。

#2


2  

Since you already have a way to calculate the difference if they are the same AM/PM, you just have to convert them to 24-hour scale:

既然你已经有了计算差异的方法,如果它们是相同的AM/PM,你只需要将它们转换为24小时尺度:

if(time.isPM())
    //add 12 hours to time

and then do your calculations (assuming they are in the same day). This is also assuming you are not just using the Java Calendar class:

然后计算一下(假设它们在同一天)。这还假设您不只是使用Java日历类:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

With Calendar, you can just call

使用日历,你可以直接调用

Calendar.getTimeInMillis()

and subtract them from each other.

然后相减。

#3


1  

For String / Date conversion and / or calculation I recommend you the utilities of Apache Commons: http://commons.apache.org/lang/api-2.6/index.html

对于字符串/日期转换和/或计算,我建议您使用Apache Commons: http://commons.apache.org/lang/api-2.6/index.html

  • StringUtils
  • stringutil
  • DateUtils
  • DateUtils
  • DateFormatUtils
  • DateFormatUtils

Always useful and no need to reinvent the wheel...

总是有用的,不需要重新发明*……

Update: I recently used another library: JodaTime which was very useful for all the date manipulation.

更新:我最近使用了另一个库:JodaTime,它对于所有的日期操作都非常有用。

#4


1  

only the format was incorrect... the line

只是格式不正确……这条线

SimpleDateFormat sdf=new SimpleDateFormat("HH:mm a"); 

should be changed to

应该更改为

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a"); 

everything should be fine after that.

之后一切都会好起来的。

#5


0  

To calculate the difference between two times in 12 hour AM/PM format, try this:

要计算12小时AM/PM格式的两次间隔,请尝试以下方法:

     try {
        SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a"); 

        Date d1 = format.parse("11:59:30 PM");
        Date d2 = format.parse("12:01:00 AM");

        System.out.println("=== Start* :: "+ timeStart);
        System.out.println("=== End*   :: "+ timerEndOrCurrent);

        long difference = d2.getTime() - d1.getTime(); 
        int days = (int) (difference / (1000*60*60*24));  
        int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
        long min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
        hours =  (hours < 0 ? -hours : hours);
        long sec = (int) (difference / 1000) % (60);

        System.out.println("=== hours :: "+ hours);
        System.out.println("=== min   :: "+ ( min < 0 ? ((min-1)+60) : min));
        System.out.println("=== sec   :: "+ ( sec < 0 ? -sec : sec));
        System.out.println("");

    } catch (ParseException e1) {
        e1.printStackTrace();
    }