0

I am using the formulae:

myDatetime = Datetime.now(); myDatetimeStr = myDatetime.format('dd/MM/YYYY'); 

enter image description here

But the values I get are next' year's date.

13:37:40:000 USER_DEBUG "Close Date" : "29/12/2016", 

How is that even possible that the : DateTime.now() is so wrong?

Debug logs are even more crazy :-/

14:19:22.746 (746983721)|VARIABLE_SCOPE_BEGIN|[163]|myDatetime|Datetime|false|false 14:19:22.747 (747018312)|VARIABLE_ASSIGNMENT|[163]|myDatetime|"2015-12-29T00:00:00.000Z" 14:19:22.747 (747028286)|STATEMENT_EXECUTE|[164] 14:19:22.747 (747037545)|HEAP_ALLOCATE|[164]|Bytes:10 14:19:22.747 (747137413)|HEAP_ALLOCATE|[164]|Bytes:10 14:19:22.747 (747169556)|VARIABLE_SCOPE_BEGIN|[164]|myDatetimeStr|String|false|false 14:19:22.747 (747188016)|VARIABLE_ASSIGNMENT|[164]|myDatetimeStr|"29/12/2016"

2
  • 1
    check if you are adding something like addyear() Commented Dec 29, 2015 at 12:12
  • thanks, not adding anything, simple 2 liner code. added debug log output to question Commented Dec 29, 2015 at 12:21

1 Answer 1

3

Use small y instead of capital Y

Datetime myDatetime = Datetime.now(); String myDatetimeStr = myDatetime.format('dd/MM/yyyy'); system.debug('=========='+myDatetimeStr); 

IF you looked into this link

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.