0

Somehow I am being reported a issue, in which following code provides date in future.

The timezone used is GMT+01:00. The numberOfDays is non negative integer.

The intention of this code is reduce the number of days from current date.

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yy",Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -numberOfDays);
Date date = calendar.getTime();
String dateStr= formatter.format(date);
System.out.println("Date : "+dateStr);

I am not able to reproduce this on my machine.

Does the Locale affect the TimeZone?

I tried to correlate to Why does a new SimpleDateFormat object contain calendar with the wrong year?, and Strange problem with timezone, calendar and SimpleDateFormat but in vain.

Please help me understand and rectify this issue.

Community
  • 1
  • 1
Nilesh
  • 557
  • 2
  • 10
  • 21
  • Works for me actually (Prints `Date : 25 Feb 11` for me; I'm on GMT). Which timezone is your system on? – adarshr Mar 07 '11 at 11:40
  • Just to clarify: where you able to reproduce the problem locally? Or did you just get an unverified report? – Joachim Sauer Mar 07 '11 at 11:42
  • Maybe is should be DAY_OF_YEAR instead of DAY_OF_MONTH ? – Damian Leszczyński - Vash Mar 07 '11 at 11:44
  • @adarshr: Works for me too, I have changed my system timezone to GMT+1.00 @Martin: Thanks, but I do not have to authority to change the implementation unless I prove the need for it. @Joachim Sauer: I am still not able to reproduce the problem – Nilesh Mar 07 '11 at 12:05

1 Answers1

2

Well, two possibilities I can think of off the top of my head:

  • The system date on the client machine is incorrect, so the calendar starts with a date in the future
  • If numberOfDays is negative, it will obviously push the date into the future

The Locale isn't directly related to the time zone - they're independent, although obviously a machine with a French locale is likely to be in a French time zone etc.

Personally I would avoid using Date/Calendar entirely and use Joda Time as a much nicer date and time API, but that wouldn't help with either of the ideas I gave above...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the reply. From what is being told, the date is correctly set and the numberOfDays is non negative integer. I can't really change the implementation without providing the cause for the bug, hence cannot use Joda Time. – Nilesh Mar 07 '11 at 12:03
  • @Nilesh: Can you print out the date *before* the call to `add` (and the value of `numberOfDays`) for diagnostic purposes? What's your relationship like with the person having the problem? – Jon Skeet Mar 07 '11 at 12:04
  • I could see the correct date on my local machines before `add`; also the numberOfDays is not negative , the client who has this problem is far than reachable, I do not have any information/access to the place where this is occurring. – Nilesh Mar 07 '11 at 12:15
  • @Nilesh: Well that's where you need to get the information, I'm afraid. I'm not surprised that numberOfDays is positive and the date is correct on the machine where you *can't* reproduce the problem :) – Jon Skeet Mar 07 '11 at 12:32