在Java中减去时间字符串

时间:2015-04-02 10:50:36

标签: java

我有两次String格式。 9:00 AM10:00 AM。我想减去它们。差异必须是1小时。我尝试了以下代码来做到这一点,但我没有得到预期的结果:

String time1 = "9:00 AM";
String time2 = "10:00 AM";
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
Date d1 = formatter.parse(time1);
Date d2 = formatter.parse(time2);
long timeDiff = d2.getTime() - d1.getTime();
Date diff = new Date(timeDiff);
System.out.println(formatter.format(diff));

我该怎么做?

4 个答案:

答案 0 :(得分:3)

使用Joda时间!见this question

DateTimeFormatter formatter = DateTimeFormat.forPattern("H:mm a");
DateTime time1 = formatter.parseDateTime("9:00 AM");
DateTime time2 = formatter.parseDateTime("10:00 AM");

Duration duration = new Duration(time1, time2);
System.out.printf("%s hour(s), %s minute(s)%n", duration.getStandardHours(), duration.getStandardMinutes());

答案 1 :(得分:2)

尝试类似:

long timeDiff = d2.getTime() - d1.getTime();
System.out.println((timeDiff / 3600000) + " hour/s " + (timeDiff % 3600000) / 60000 + " minutes");
Output
1 hour/s 5 minutes

答案 2 :(得分:1)

您的方法似乎是正确的。 唯一不应该做的是,将timeDiff更改为新日期。 而是将其更改为在几分钟和几小时内获得时间,如下所示 例如: -

long diffMinutes = timeDiff / (60 * 1000) % 60;
long diffHours = timeDiff / (60 * 60 * 1000) % 24;
System.out.println(diffHours+" hours "+ diffMinutes +" minutes");

这里是完整的代码如果你想更准确地显示时差,你可以参考,

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;

/* Name of the class has to be "Main" only if the class is public. */

class TimeDiffTester
{
    public static String show(long value, String showAs) {
        if(value == 0) {
            return "";
        } else {
            return Math.abs(value) +" "+showAs+" ";
        }
    }
    public static void getDifferenceInTime(String time1, String time2) throws java.lang.Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
        Date d1 = formatter.parse(time1);
        Date d2 = formatter.parse(time2);
        long timeDiff = d2.getTime() - d1.getTime();

        long diffDays = timeDiff / (24 * 60 * 60 * 1000);
        long diffHours = timeDiff / (60 * 60 * 1000) % 24;
        long diffMinutes = timeDiff / (60 * 1000) % 60;
        long diffSeconds = timeDiff / 1000 % 60;

        String difference = show(diffDays, "days") + show(diffHours, "hours") + show(diffMinutes, "minutes") + show(diffSeconds, "seconds");
        if(diffDays < 0 || diffHours < 0 || diffMinutes < 0 || diffSeconds < 0) {
            System.out.println("-"+difference); 
        } else {
            System.out.println("+"+difference); 
        }
    }
    public static void main (String[] args) throws java.lang.Exception
    {
            String time1 = "4:30 PM";
            String time2 = "5:00 PM";

            getDifferenceInTime(time1,time2);

    }
}

答案 3 :(得分:0)

这是最好的例子。

1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day


public static void main(String[] args) {

        String dateStart = "01/14/2012 09:29:58";
        String dateStop = "01/15/2012 10:31:48";

        //HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

        Date d1 = null;
        Date d2 = null;

        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            //in milliseconds
            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.");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

请查找此示例的参考链接

http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/