2

This is my first question, so please be gentle with me! I am having a problem with some pre-existing java code. 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. 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".

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!

4

3 回答 3

10

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

于 2013-11-05T10:09:56.230 回答
1

如前所述,MONTH 是 0-11。您的代码没有抛出异常,因为 lenient 的默认值为 true。您应该将其设置为 false(除非您明确想要这种行为)以更容易地检测到这种情况:

calendar1.setLenient(false);
calendar2.setLenient(false);
于 2013-11-05T10:28:28.800 回答
-1

它实际上并没有回答您的问题,但是如果您只需要 的值diff,则无需使用Calendar实例,但是您可以将代码完全替换为:

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

如果您确实需要Calendar对象进行其他操作,仍然不需要解析日期的字符串表示,您可以简单地将 设置为一个操作Calendar的值:Date

Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(startdate.getTime());
于 2013-11-05T10:41:29.990 回答