简体   繁体   English

Java Calendar为两个不同的日期返回相同的毫秒值

[英]Java Calendar returns same millisecond value for two different dates

This is my first question, so please be gentle with me! 这是我的第一个问题,所以请温柔地对待我! I am having a problem with some pre-existing java code. 我遇到了一些预先存在的java代码的问题。 It is pretty simple, you pass it two dates in the format "2013-10-31", it then calculates the ms difference between the two values and then does some more calculations after that. 这很简单,你以格式“2013-10-31”传递两个日期,然后计算两个值之间的ms差异,然后再进行一些计算。 The problem is that every now and again, even though two different dates are passed, they both have the same millisecond value. 问题在于,即使传递了两个不同的日期,它们也会一次又一次地具有相同的毫秒值。 An example of this is if you pass "2013-10-31" and "2013-11-01", it returns the difference as 0. The ms values both being "1385856000000". 例如,如果您传递“2013-10-31”和“2013-11-01”,它会将差值返回为0.毫秒值均为“1385856000000”。

Code is: 代码是:

  public int getTotalStartEndTime( java.sql.Date startdate, java.sql.Date enddate, java.sql.Time starttime, java.sql.Time endtime )

{ {

if(startdate != null & enddate != null && starttime !=null && endtime!= null){

     Calendar calendar1 = Calendar.getInstance();
     Calendar calendar2 = Calendar.getInstance();
    int styr = Integer.parseInt(startdate.toString().substring(0,startdate.toString().indexOf("-")),10);
    int stmm = Integer.parseInt(startdate.toString().substring(startdate.toString().indexOf("-")+1,startdate.toString().lastIndexOf("-")),10);
    int stdd = Integer.parseInt(startdate.toString().substring(startdate.toString().lastIndexOf("-")+1),10);         
    int enyr = Integer.parseInt(enddate.toString().substring(0,enddate.toString().indexOf("-")),10);
    int enmm = Integer.parseInt(enddate.toString().substring(enddate.toString().indexOf("-")+1,enddate.toString().lastIndexOf("-")),10);
    int endd = Integer.parseInt(enddate.toString().substring(enddate.toString().lastIndexOf("-")+1),10);

    //calendar1.set(styr, stmm, stdd);
    calendar1.set(Calendar.YEAR, styr);
    calendar1.set(Calendar.MONTH, stmm);
    calendar1.set(Calendar.DAY_OF_MONTH, stdd);
    calendar1.set(Calendar.HOUR, 0);
    calendar1.set(Calendar.MINUTE, 0);
    calendar1.set(Calendar.SECOND, 0);
    calendar1.set(Calendar.MILLISECOND,0);
    //calendar2.set(enyr, enmm, endd);
    calendar2.set(Calendar.YEAR, enyr);
    calendar2.set(Calendar.MONTH, enmm);
    calendar2.set(Calendar.DAY_OF_MONTH, endd);
    calendar2.set(Calendar.HOUR, 0);
    calendar2.set(Calendar.MINUTE, 0);
    calendar2.set(Calendar.SECOND, 0);
    calendar2.set(Calendar.MILLISECOND,0);

    long milliseconds1 = calendar1.getTimeInMillis();
    long milliseconds2 = calendar2.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;

Any help would be greatly appreciated, as I cannot work out what is happening! 任何帮助将不胜感激,因为我无法弄清楚发生了什么!

日历月份为0-11,在您的代码中,您从字符串解析日期,而第10个月将转换为11月,其中没有31天并且设置为12月1日。

As it has been said, MONTH is 0-11. 如上所述,MONTH为0-11。 Your code didn't throw an exception since the default value of lenient is true. 您的代码没有抛出异常,因为lenient的默认值为true。 You should set it to false (unless you explicitly want this behavior) to detect this kind of situation more easily : 您应该将其设置为false(除非您明确要求此行为)以更轻松地检测此类情况:

calendar1.setLenient(false);
calendar2.setLenient(false);

It's not actually answering your question, but if all you need is the value of diff , there is no need to work with Calendar instances, but you can replace your code entirely with: 它实际上并没有回答你的问题,但如果你只需要diff的值,就不需要使用Calendar实例,但你可以完全用以下代码替换你的代码:

long diff = enddate.getTime() - startdate.getTime();

If you actually need the Calendar objects for other operations, there is still no need to parse the string representation of the dates, you can simply set the Calendar to the Date value with one operation: 如果您确实需要Calendar对象进行其他操作,则仍然无需解析日期的字符串表示形式,您只需使用一个操作将Calendar设置为Date值:

Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(startdate.getTime());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM