3

I have these two strings as input strings

03/12/16
03/14/16

I make a single SimpleDateFormatter object to get the date difference like this

DateFormat formatter = new SimpleDateFormat("MM/dd/yy",Locale.US);
Date dateChkIn = formatter.parse("03/12/16");
System.out.println("Checkin date at nights check - "+dateChkIn.toString());
Date dateChkOut = formatter.parse("03/14/16");
System.out.println("Checkout date at nights check - "+dateChkOut.toString());
Long numberOfNghtsCalc = ((dateChkOut.getTime() - dateChkIn.getTime()) / 86400000L);
System.out.println("number of nigts calculated - "+ numberOfNghtsCalc);

Below is my output in the server

Checkin date at nights check - Sat Mar 12 00:00:00 EST 2016
Checkout date at nights check - Mon Mar 14 00:00:00 EDT 2016
number of nigts calculated - 1

Expected output is number of nigts calculated - 2 Please note the two different Time Zones which returned from the formatter

Update : 13-Mar-2016 is considered as DST changing date. Therefore my calculation is getting wrong. (divide by 86400000L)

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
  • Start by using either Java 8's Time API or JodaTime to calculate the difference between dates ... did you happen to cross over a daylight savings boundary? – MadProgrammer Sep 25 '15 at 06:58
  • @MadProgrammer I appreciate your comment. But my system is using Java 1.5 . This code is in my production environment. All these time; it worked perfectly. Suddenly this was occured. – Jude Niroshan Sep 25 '15 at 07:00
  • 1
    Well, Joda-Time supports Java 5, but you could even use `Calendar` – MadProgrammer Sep 25 '15 at 07:01
  • try add `formatter.setTimeZone(TimeZone.getTimeZone("GMT"));` below the `SimpleDateFormat` – cw fei Sep 25 '15 at 07:30
  • @cwfei It might not work. All servers are set to US time zone. EDT. I want to do all calculations upon that. – Jude Niroshan Sep 25 '15 at 07:32
  • 2
    you may read this http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices – cw fei Sep 25 '15 at 08:06

4 Answers4

2

I found the solution. I got to know that best solution would be use Joda Time. But with least change in the existing code, I think this tricky way will be better. Please edit this answer if you see any drawbacks.

    List<Date> dates = new ArrayList<Date>();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(dateChkIn);

    Date result = null;
    while (calendar.getTime().before(dateChkOut)){
            result = calendar.getTime();


            dates.add(result);
            calendar.add(Calendar.DATE, 1);
    }
    System.out.println("DAYS CALCULATED BY NEW SOLUTION: " + dates.size());
    newNumberOfNghtsCalc = (long) dates.size();
    dates.clear();

This has actually reduced the running time!

Time taken for code in Question : 275000 (in nano seconds)

Time taken for New solution: 103000

Jude Niroshan
  • 4,280
  • 8
  • 40
  • 62
1

on next 13 March 2016 change hour by EST to EDT. So SimpleDateFormat takes care of this change.

Go here

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • I am sorry. I don't know much about **Daylight saving time**. This issue was reported in our production. Should I need to do any code level changes to handle this? Is this a very rare occation? How should I inform this to my US client? – Jude Niroshan Sep 25 '15 at 07:11
  • @JudeNiroshan: Not rare! Every year you have the same issue when change EST -> EDT and viceversa. The same issue you have if you work with different time zone (i.e. Europe vs America). – Joe Taras Sep 25 '15 at 07:29
  • But my question is why **SimpleDateFomatter** returned two different timezones ? EST for 12th and EDT for 16th ? – Jude Niroshan Sep 25 '15 at 07:31
  • @Jude: SimpleDateFormatter returns the same time zone, but time zone change in that area. So SDF is sensible to this change. – Joe Taras Sep 25 '15 at 07:40
1

The difference is of caused by the Daylight Saving Time, which for 2016 is scheduled as March 13, 2016. Here you can read a short article on the difference between EDT and EST.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
1

java.time

I recommend you use the modern, java.time date-time API.

Demo:

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/uu", Locale.ENGLISH);
        LocalDate dateChkIn = LocalDate.parse("03/12/16", dtf);
        LocalDate dateChkOut = LocalDate.parse("03/14/16", dtf);
        long days = ChronoUnit.DAYS.between(dateChkIn, dateChkOut);
        System.out.println(days);
    }
}

Output:

2

Learn about the modern date-time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110