【问题标题】:Calendar method getTimeInMillis() returning wrong value日历方法 getTimeInMillis() 返回错误值
【发布时间】:2016-12-06 11:25:47
【问题描述】:

我有一个验证方法,它将接受两个输入作为开始日期和结束日期,并检查结束日期是否有效,即在开始日期之后。 我使用日历方法getTimeInMillis() 来获取两个日期的毫秒数并得到差异。如果差值为负,则其无效结束日期,即结束日期早于开始日期。 结束日期的毫秒值应始终大于开始日期。但在某些情况下,getTimeInMillis() 在开始日期的情况下提供更大的价值。

这是我的代码:

public class ValidationProcessor {
    private void testTimeDiff(String startDate, String endDate) throws Exception {
        Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        // Set the dates for calendars
        cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]),
            Integer.parseInt(startDate.split("-")[2]));
        cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]),
            Integer.parseInt(endDate.split("-")[2]));
        long milis1 = cal1.getTimeInMillis();
        long milis2 = cal2.getTimeInMillis();
        System.out.println("milis1::: " + milis1);
        System.out.println("milis2::: " + milis2);
        long diff = milis2 - milis1;
        System.out.println("diff::: " + diff);
        if (diff <= 0) {
            throw new Exception("Invalid End Date. End Date Must Be After Start Date");
        }
    }

    public static void main(String args[]) {
        ValidationProcessor test = new ValidationProcessor();
        try {
            test.testTimeDiff("2015-01-31", "2015-02-01");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出是:

milis1::: 1425367717308
milis2::: 1425194917308
diff::: -172800000

java.lang.Exception: Invalid End Date. End Date Must Be After Start Date

这里 2015-01-31 是开始日期,2015-02-01 是结束日期。 由于开始日期(2015-01-31)以毫秒为单位的值比结束日期(2015-02-01)更大,因此它给出了错误的结果。

那么这是Calendar 类的方法getTimeInMillis() 的问题还是我做错了什么?

【问题讨论】:

    标签: java date


    【解决方案1】:

    这可能是因为Calendar.getInstance() 为您提供了当前 时间戳(填写了所有字段,例如小时、分钟等),而您没有'不明确清除那些额外的字段(你只设置年、月和日期)。

    另外,请注意 dat Calendar.set(int year, int month, int day) 使用 基于 0 的月份(例如 Jan = 0、Feb = 1 等),因此您的转换目前是错误的。

    更新:我认为这是两者的结合;您的测试日期 2015-01-31 被解析为 2015 年 2 月 31 日,这当然是无效的,导致意外的 getTimeInMillis() 值。

    【讨论】:

      【解决方案2】:

      使用cal1.getTime().getAfter(cal2.getTime());

      更新 1

      要填满你的日历,你应该像下面那样做

      //cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]), Integer.parseInt(startDate.split("-")[2]));
      //cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]), Integer.parseInt(endDate.split("-")[2]));
      
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      cal1.setTime(df.parse(startDate));
      cal2.setTime(df.parse(endDate));
      

      那么您的原始代码也可以正常工作。

      更新 2

      而您可以避免将日历用于此特定用途并直接使用日期对象。

      Date date1 = df.parse(startDate);
      Date date2 = df.parse(endDate);
      
      long milis1 = date1.getTime();
      long milis2 = date2.getTime();
      

      【讨论】:

        【解决方案3】:

        一个问题是,如果我们将月份设置为 -1,那么它会给出正确的结果。 喜欢,

        cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]) -1,
                    Integer.parseInt(startDate.split("-")[2]));
        cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]-1),
                    Integer.parseInt(endDate.split("-")[2]));
        

        它给出,差异为 86400000

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-16
          • 2018-11-26
          相关资源
          最近更新 更多