【问题标题】:Comparing two time periods using getTime not working使用 getTime 比较两个时间段不起作用
【发布时间】:2018-03-09 07:35:55
【问题描述】:

我是 Java 新手。我得到了 3 次,格式为“HH:mm:ss”。我应该接受一个输入(以整数(并且必须以分钟为单位))将其添加到第三次。如果时间之和小于第 1 次和第 2 次(第 1 次

但是,我将日期映射到的双变量的值正在返回 有时为负值(请检查输出)。

有人能帮我理解为什么吗?谢谢。

import java.util.*;
import java.text.*;

public class TimeTrial2 {
    public static void main(String q[]) throws ParseException
    {
    Scanner x=new Scanner(System.in);

    SimpleDateFormat y=new SimpleDateFormat("HH");
    SimpleDateFormat k=new SimpleDateFormat("HH:mm:ss");

    String b=x.nextLine();
    Date c=k.parse(b);//1st Time
    String d=x.nextLine();
    Date e=k.parse(d);//2nd Time
    String f=x.nextLine();
    Date g=k.parse(f);//Time to be added to the input
    int a=x.nextInt();//Input time i
    a=a/60;

    Date z=y.parse(Integer.toString(a));
    double p=(z.getTime()+g.getTime()); 
    double r=c.getTime();
    double s=e.getTime();

    System.out.println(p+"\n"+r+"\n"+s); `//to check the values of p,r and s`
    if(r>=p)
    {
        System.out.println("1");
    }

    else if(s>=p)
    {
        System.out.println("2");
    }

    else
    {
        System.out.println("FALSE!");
    }
}

}

输入

4:30:00

6:00:00

4:00:00

60

这是我的输出

-2.16E7

-3600000.0

1800000.0

1

编辑:添加 330 分钟(格林威治标准时间 +5:30)可以解决问题。 但是,我建议使用 Ole V.V 的答案,因为它更容易。我不知道存在这样的日期类和方法。谢谢大家的帮助。

【问题讨论】:

  • 要求我们调试您的代码不是问题,请参考How to ask
  • 当getTime返回long时,你为什么使用double
  • 今天是学习使用 IDE 附带的调试器的好日子。
  • 在将代码发布到 Stack Overflow 并要求我们阅读和理解时,您能想出更多解释性变量名称吗?
  • @OleV.V.:将确保我正确命名变量。很抱歉给您带来麻烦。

标签: java datetime time


【解决方案1】:

考虑如何将时间添加到日期。我尝试了以下代码来为日期添加分钟并且它有效。

Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(g);
calendar.add(GregorianCalendar.MINUTE, a);

输入:

g = 5:00:00
a = 40

输出:

Thu Jan 01 05:00:00 IST 1970
Thu Jan 01 05:40:00 IST 1970

【讨论】:

    【解决方案2】:
    import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.Scanner;
    
    public class TimeTrial3 {
    
        public static void main(String... commandLineArguments) {
            Scanner consoleInput = new Scanner(System.in);
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm:ss");
    
            String time1String = consoleInput.nextLine();
            LocalTime time1 = LocalTime.parse(time1String, timeFormatter);
            String time2String = consoleInput.nextLine();
            LocalTime time2 = LocalTime.parse(time2String, timeFormatter);
            if (time2.isBefore(time1)) {
                System.err.println("First time must be before or equal to second time, they were"
                        + time1 + " and " + time2);
                System.exit(1);
            }
    
            String timeToAddString = consoleInput.nextLine();
            LocalTime timetoAdd = LocalTime.parse(timeToAddString, timeFormatter);
            int minutesToAdd = consoleInput.nextInt();
            LocalTime timeToCompare = timetoAdd.plusMinutes(minutesToAdd);
            if (timeToCompare.isBefore(time1))
            {
                System.out.println("1");
            }
            else if (timeToCompare.isBefore(time2))
            {
                System.out.println("2");
            }
            else
            {
                System.out.println("False");
            }
        }
    
    }
    

    您的代码存在问题

    • Date 类不太适合仅表示一天中的某个时间。
    • DateSimpleDateFormat 类早已过时,而后者尤其是出了名的麻烦。我建议你根本不要使用它们。相反,我使用并热烈推荐 java.time,现代 Java 日期和时间 API。使用起来感觉好多了。
    • 当您可以将库类用于日期和/或时间时,请执行此操作。不要使用doublelong
    • 观察结果,并不是真正的问题:您所在的时区似乎与 UTC 相差 -05:30(如斯里兰卡或印度)?例如,您输入的 4:30:00 被解析为 1970 年 1 月 1 日(纪元日)偏移量 -5:50 的这个时间,等于纪元前 1 小时,所以它输出为 -3600000.0,因为 - 3600 秒等于 -1 小时。只要您的所有时间都处于相同的偏移量,这本身就不是问题。然后你仍然可以使用beforeafter 来比较它们,就像harsha kumar Reddy 在another answer 中所说的那样。
    • 当您尝试从两个Date 对象中添加毫秒时,问题就来了。第三次 4:00:00 被解析为纪元前 1 小时 30 分钟,而 60 分钟被解析为纪元前 4 小时 30 分钟。添加两个时,您预计时间为 5:00:00(在您的时区)。相反,您会在纪元前 6 小时得到打印为-2.16E7(在我的计算机上,由于我在不同的时区,我得到了不同的错误结果)。
    • 通过将输入的分钟数除以 60 得到小时数,您将失去精度。例如,如果输入是 90 分钟(一个半小时),则除法后只有 1 小时。
    • 正如评论中提到的,您应该使用解释性变量名称。你可能对你选择的名字有一个想法,但我不明白。

    链接

    Oracle tutorial: Date Time 解释如何使用java.time

    【讨论】:

    • 非常感谢您的详细解释。这真的很有见地。一件事,为了纠正时区偏移,我可以添加 UTC 和 IST(我的时区)之间的差异吗?
    • 如果你想这样做,我相信你应该通过其setTimeZone 方法将每个格式化程序的时区设置为UTC。听起来它会起作用,但不能保证。一个潜在的缺点是,如果您打印您的 Date 对象,输出将会令人困惑,因为他们的 toString 方法仍将使用您的本地时区。
    • 但我不明白您为什么介意使用现代且更好的课程。在任何情况下,使用Date,一个时间点来表示要添加的 60 分钟,在概念上是不正确的,并且势必会使任何试图阅读您的代码的人感到困惑。
    • 我没有。如前所述,我是 Java 的新手,我们的培训师给了这个问题,让我自己解决。有人告诉我这个问题可以通过 SimpleDateFormat 解决。但是,事实证明,您的解决方法要容易得多。非常感谢您的帮助。
    • 很抱歉,如果我通过提供工作代码提供了太多帮助。当然,我无意破坏它并剥夺你的学习成果。
    【解决方案3】:

    尝试使用 Date 类的 equals() , before() , after() 方法。

    参考 java Api 文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多