You need to sign in to do that
Don't have an account?
Parra d'Andert
Issues with date format for [28-31] Dec
Hi,
I have an issue with Date formating for the following dates 28-Dec-2014, 29-Dec-2014, 30-Dec-2014, 31-Dec-2014. Whenever I format one of these dates using either apex of Visualforce, a year is added to the formated date (31 Dec 2014 becomes 31 Dec 2015)
Other dates work fine.
This is really easy to reproduce using execute anonymous:
Date testDate = Date.newInstance(2014, 12, 31);
System.debug('Date.newInstance(2014, 12, 31) : ' + testDate);
Output: Date.newInstance(2014, 12, 29) : 2014-12-30 00:00:00
System.debug('year : ' + testDate.year() + ' - month : ' + testDate.month() + ' - day : ' + testDate.day());
Output: year : 2014 - month : 12 - day : 30
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()));
Output: Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : 2014-12-29 23:00:00
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM YYYY") : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format('dd MMM YYYY'));
Output: Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM YYYY") : 30 Dec 2015
The visualforce formating rendering the same is:
<apex:outputText value="{0, date, dd MMM YYYY}"><apex:param value="{!testDate}"/></apex:outputText>
Can someone please explain me what's going on?
Thanks
I have an issue with Date formating for the following dates 28-Dec-2014, 29-Dec-2014, 30-Dec-2014, 31-Dec-2014. Whenever I format one of these dates using either apex of Visualforce, a year is added to the formated date (31 Dec 2014 becomes 31 Dec 2015)
Other dates work fine.
This is really easy to reproduce using execute anonymous:
Date testDate = Date.newInstance(2014, 12, 31);
System.debug('Date.newInstance(2014, 12, 31) : ' + testDate);
Output: Date.newInstance(2014, 12, 29) : 2014-12-30 00:00:00
System.debug('year : ' + testDate.year() + ' - month : ' + testDate.month() + ' - day : ' + testDate.day());
Output: year : 2014 - month : 12 - day : 30
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()));
Output: Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : 2014-12-29 23:00:00
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM YYYY") : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format('dd MMM YYYY'));
Output: Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM YYYY") : 30 Dec 2015
The visualforce formating rendering the same is:
<apex:outputText value="{0, date, dd MMM YYYY}"><apex:param value="{!testDate}"/></apex:outputText>
Can someone please explain me what's going on?
Thanks
There is an error with your formating string, I think that you'll need to replace YYYY by yyyy
Date testDate = Date.newInstance(2014, 12, 31);
System.debug('Date.newInstance(2014, 12, 31) : ' + testDate);
System.debug('year : ' + testDate.year() + ' - month : ' + testDate.month() + ' - day : ' + testDate.day());
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()));
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM yyyy") : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format('dd MMM yyyy'));
<apex:outputText value="{0, date, dd MMM yyyy}"><apex:param value="{!testDate}"/></apex:outputText>
All Answers
There is an error with your formating string, I think that you'll need to replace YYYY by yyyy
Date testDate = Date.newInstance(2014, 12, 31);
System.debug('Date.newInstance(2014, 12, 31) : ' + testDate);
System.debug('year : ' + testDate.year() + ' - month : ' + testDate.month() + ' - day : ' + testDate.day());
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()) : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()));
System.debug('Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format("dd MMM yyyy") : ' + Datetime.newInstance(testDate.year(), testDate.month(), testDate.day()).format('dd MMM yyyy'));
<apex:outputText value="{0, date, dd MMM yyyy}"><apex:param value="{!testDate}"/></apex:outputText>