当前位置: 首页 > 面试题库 >

查找Java中的天差

易镜
2023-03-14
问题内容

在咨询了几个论坛之后,我最终使用下面的代码来查找时间差异。但是,我发现逻辑上存在问题(可能是我的视线吗?)。我看到11到14天和11到15天之间的差异是相同的。这怎么可能?

Date createdDate = new Date((2013 + 1900), (1 + 1), 11);
Date expirationDate = new Date((2013 + 1900), (1 + 1), 11);
for (int i = 11; i < 20; i++) {
    expirationDate.setDate(i);

    System.out.println("11 to " + i + " = "
            + (int) (expirationDate.getTime() - createdDate.getTime())
            / (1000 * 60 * 60 * 24));
}

输出为:

11 to 11 = 0
11 to 12 = 1
11 to 13 = 2
11 to 14 = 3
11 to 15 = 3
11 to 16 = 4
11 to 17 = 5
11 to 18 = 6
11 to 19 = 7

问题答案:

使用浮动,我看到了问题。使用时间戳似乎不是找到两个日期之间的天差的好方法。

11至11 = 0.0
11至12 = 1.0
11至13 = 2.0
11至14 = 3.0
11至15 = 3.9583333
11至16 = 4.9583335
11至17 = 5.9583335
11至18 = 6.9583335
11至19 = 7.9583335


展望未来,我发现确定日期差异的最确定的方法是

Calendar cre_calendar = new GregorianCalendar((2013), (1), 11);
        Calendar exp_calendar = new GregorianCalendar((2013), (1), 19);
        Calendar maxDays = new GregorianCalendar(((2013)), (12), 31);

        if (exp_calendar.get(Calendar.DAY_OF_YEAR) < cre_calendar
                .get(Calendar.DAY_OF_YEAR)) {
            System.out
                    .println((exp_calendar.get(Calendar.DAY_OF_YEAR) + maxDays
                            .get(Calendar.DAY_OF_YEAR))
                            - cre_calendar.get(Calendar.DAY_OF_YEAR));
        } else {
            System.out.println((exp_calendar.get(Calendar.DAY_OF_YEAR))
                    - cre_calendar.get(Calendar.DAY_OF_YEAR));
        }


 类似资料:
  • 问题内容: 您如何找到Java中一个月中的天数? 问题答案: 在Calendar对象上设置年和月,然后用于返回最后一天:

  • 问题内容: 如何使用一年中的某天找到该月的某天? 例如,第16天是1月16日。第35天是2月4日 我的程序提示输入一年中的某一天(1-365)。 因此输入可以是252天和2017年 我已经为leap年设置了一个布尔值。 我已经使用if语句将其分为12个月(1月-12月EX:如果日期输入是1月的1-31日)。 我的程序快要完成了,这是我的最后一步,但是我不知道该怎么做。 如何在不使用API​​或导入

  • 在Java8中,我发现 给出一个月的第一个星期一的时间,以及 给出特定日期后的下一个星期一。 但是我想在具体日期后找到第n个星期一。 例如,我想要2017-06-06之后的第二个星期一,应该是2017-06-19,在哪里 会给我2017-06-12和 当然没有第n天的指标参数。它将给出下一个第一个星期一,即2017年6月12日。 如何在不循环的情况下计算它?

  • 环境:Java1.8,VM Cloudera QuickStart。我有从csv文件到Hadoop hdfs的数据。每一行代表一条公共汽车路线。

  • 问题内容: 如何提出一个存储过程来选择过去30天的结果? 问题答案: 用适当的列名称替换第一个GETDATE()。

  • 我正在尝试使用openweather API查找7天的天气预报,但是当我调用openweather API时,数据不会来自当前位置,任何人都可以指导我找到正确的API来获取当前位置的天气预报。 到目前为止使用的API 参考链接: https://openweathermap.org/forecast5 https://openweathermap.org/api/one-call-api 请求:<