13

Hi I am executing following code snippet and after 26/12/2015, date shifted to one year ahead of expected.

Date startDate = Date.parse('12/24/2015');
Date endDate = Date.parse('12/30/2015');

while(startDate <= endDate){
    Datetime localDateTime = startDate;
    startDate = startDate.addDays(1);
    System.debug('~~~~~localDateTime'+localDateTime);
    System.debug('~~~~~localDateTime'+localDateTime.format('MM/dd/YYYY'));
}

And following is the debug

19:14:17.040 (40027257)|EXECUTION_STARTED
19:14:17.040 (40036021)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
19:14:17.040 (40989828)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-24 00:00:00
19:14:17.041 (41059958)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/24/2015
19:14:17.041 (41128835)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-25 00:00:00
19:14:17.041 (41163269)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/25/2015
19:14:17.041 (41222037)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-26 00:00:00
19:14:17.041 (41254864)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/26/2015
19:14:17.041 (41312303)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-27 00:00:00
19:14:17.041 (41345356)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/27/2016
19:14:17.041 (41402820)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-28 00:00:00
19:14:17.041 (41434519)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/28/2016
19:14:17.041 (41493306)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-29 00:00:00
19:14:17.041 (41525359)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/29/2016
19:14:17.041 (41582636)|USER_DEBUG|[7]|DEBUG|~~~~~localDateTime2015-12-30 00:00:00
19:14:17.041 (41613956)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime12/30/2016

Any Idea why it is happening ?

Thanks

  • It appears as though the year switch at the same time the year would have switched for the endDateTime when it would have gone to Jan 2nd 2016. It's as though the function couldn't handle checking the difference between the two and concluded you wanted the difference to be between dates in the same year. I know that doesn't explain it, but it's the day after Jan 1st of the endDate when it occurred is what seems significant to me. Perhaps there's more to your code than what you've posted that would add context? – crmprogdev Dec 25 '15 at 15:18
  • @crmprogdev when you execute this code snippet only in developer console, you will find the same debug logs. problem is just in the format method for dateTime. But if you just do startDate.format(), it gives right answer. I dont know what is the problem, but it seems bug to me. – ashishcloud Dec 25 '15 at 16:27
  • I agree, this is weird, a simpler code fragment of: Datetime dt1 = DateTime.newInstance(Date.newInstance(2015,12,30),Time.newInstance(0,0,0,0)); system.debug(Logginglevel.info,dt1.format('YYYY-MM-dd')); yields 2016-12-30 ! in DC on na2 pod - I'd say a bug in format(..) – cropredy Dec 25 '15 at 19:23
17

Actually... the datetime format is right.

The capital Y merge field is the WEEK YEAR - which from the 27th December 2015 is IN 2016!

The lower case y merge field is the CALENDAR YEAR - which will be 2015 as expected.

Change your line to

System.debug('~~~~~localDateTime'+localDateTime.format('MM/dd/yyyy'));

and you will see your logs play properly.

Notes: The Datetime.Format uses the Java SimpleDateFormat library/technology, documentation for which you can find here.

  • 2
    FYI this is because there are 52.14 weeks in a year... so the last 4 days fall "after" the last full week of the year. Happy Christmas everyone! – Simon Lawrence Dec 25 '15 at 21:37
  • 3
    oh damn -- I should have thought of that. +1 for you – cropredy Dec 25 '15 at 22:29
1

It appears there are a couple of things going on with your code. First, note that you're making the conversion to DateTime BEFORE you do apply the addDays() method. Next, note that you're not applying a complete date-time string for what you want to retrieve, instead only a date format string. Here's what I modified your code to and the results I obtained when I did.

Date startDate = Date.parse('12/24/2015');
Date endDate = Date.parse('12/30/2015');

while(startDate <= endDate){
    startDate = startDate.addDays(1);
    Datetime localDateTime = startDate;
    System.debug('~~~~~localDateTime'+localDateTime);
    System.debug('~~~~~localDateTime'+localDateTime.format('MM/dd/yyyy HH:mm:ss', 'America/New_York'));
}

In doing the above, I don't get the change in years as you did. Instead, my results stop on 12/30/15 as expected. Perhaps using the Date class instead of the DateTime class would be a better choice for your purposes to obtain the results you're looking for.

12:27:20.033 (33312354)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:27:20.034 (34356431)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-25 00:00:00
12:27:20.034 (34455201)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/24/2015 19:00:00
12:27:20.034 (34575676)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-26 00:00:00
12:27:20.034 (34647444)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/25/2015 19:00:00
12:27:20.034 (34757811)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-27 00:00:00
12:27:20.034 (34825554)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/26/2015 19:00:00
12:27:20.034 (34934603)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-28 00:00:00
12:27:20.035 (35007963)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/27/2015 19:00:00
12:27:20.035 (35119893)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-29 00:00:00
12:27:20.035 (35187075)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/28/2015 19:00:00
12:27:20.035 (35296782)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-30 00:00:00
12:27:20.035 (35362918)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/29/2015 19:00:00
12:27:20.035 (35470083)|USER_DEBUG|[8]|DEBUG|~~~~~localDateTime2015-12-31 00:00:00
12:27:20.035 (35535528)|USER_DEBUG|[9]|DEBUG|~~~~~localDateTime12/30/2015 19:00:00

Edited to add, I just noticed the last debug log entry shows 12/31 vs 12/30. Adding the additional day would be expected in a while loop since unlike a for loop, it would go through one additional loop while the value is equal to 12/30/15, thus adding an additional day which would display as 12/31 in the local time format. Why it didn't get it exactly right, isn't clear to me, but it seems consistent. One appears to be displaying in GMT and the other in EST. I ran this both as Anonymous Apex from Eclipse and also from the Console in another org where I obtained the same results.

  • The bit you missed was the fact that you wrote "yyyy" instead of "YYYY". Case sensitivity matters. – sfdcfox Dec 28 '15 at 18:07
  • 1
    @sfdcfox LOL! As usual, Apex is not case sensitive except when it is! Nice catch! But, I suspect this is a Java date-time method syntax issue and totally unrelated to Apex. ;) Oh, I now see Simon's answer confirming my suspicions. – crmprogdev Dec 28 '15 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.