0

I came across a weird scenario. The format method is replacing month with '00' in its result.

A portion of my code: (Executed Anonymously to test)

System.Debug(logginglevel.error,'****m.date_approved__c***'+m.date_approved__c);

if(m.date_approved__c!=null) {
    System.Debug(logginglevel.error,'****year***'+m.date_approved__c.year());
    System.Debug(logginglevel.error,'****month***'+m.date_approved__c.month());
    System.Debug(logginglevel.error,'****day***'+m.date_approved__c.day());

    Datetime dt = DateTime.newInstance(m.date_approved__c.year(), m.date_approved__c.month(),m.date_approved__c.day());
    System.Debug(logginglevel.error,'****date before formatting***'+dt);

    approveddate = dt.Format('mm/dd/yyyy');
}

System.Debug(logginglevel.error,'****approveddate***'+approveddate);`

Debug Results

11:24:31.35 (85764301)|USER_DEBUG|[138]|ERROR|****m.date_approved__c***2016-06-24 00:00:00
11:24:31.35 (85826327)|USER_DEBUG|[140]|ERROR|****year***2016
11:24:31.35 (85873389)|USER_DEBUG|[141]|ERROR|****month***6
11:24:31.35 (85950679)|USER_DEBUG|[142]|ERROR|****day***24
11:24:31.35 (86109856)|USER_DEBUG|[144]|ERROR|****date before formatting***2016-06-24 07:00:00
11:24:31.35 (86188235)|USER_DEBUG|[149]|ERROR|****approveddate***00/24/2016

I tested this method separately, and it is working fine.

MyObj__c m = [select id, date_approved__c from MyObj__C WHERE Id = 'a033900000EU9LN'];
Datetime dt = DateTime.newInstance(m.date_approved__c.year(), m.date_approved__c.month(),m.date_approved__c.day());

System.Debug(logginglevel.error,'****date before formatting***'+dt);
System.Debug(logginglevel.error,'*****testing format method******'+dt.Format('MM/dd/yyyy'));`

Debug result:

11:27:41.1 (6791165)|USER_DEBUG|[3]|ERROR|****date before formatting***2016-06-24 07:00:00
11:27:41.1 (6886491)|USER_DEBUG|[4]|ERROR|*****testing format method******06/24/2016

I used the same record in both the cases

Any thoughts?

1

1 Answer 1

3

Look at Java SimpleDateFormat. These format strings are case-sensitive, as different casings of the same letter can have different meanings.

m outputs Minute in hour.

M outputs Month in year.

So make sure you use MM when trying to output the month, and only use mm if you are trying to output the minutes.


SimpleDateFormat patterns

letter - description - type - example

  • G - Era designator - Text - AD
  • y - Year - Year - 1996; 96
  • M - Month in year - Month - July; Jul; 07
  • w - Week in year - Number - 27
  • W - Week in month - Number - 2
  • D - Day in year - Number - 189
  • d - Day in month - Number - 10
  • F - Day of week in month - Number - 2
  • E - Day in week - Text - Tuesday; Tue
  • a - Am/pm marker - Text - PM
  • H - Hour in day (0-23) - Number - 0
  • k - Hour in day (1-24) - Number - 24
  • K - Hour in am/pm (0-11) - Number - 0
  • h - Hour in am/pm (1-12) - Number - 12
  • m - Minute in hour - Number - 30
  • s - Second in minute - Number - 55
  • S - Millisecond - Number - 978
  • z - Time zone - General time zone - Pacific Standard Time; PST; GMT-08:00
  • Z - Time zone - RFC 822 time zone - -0800
3
  • Thank you, I didn't notice that I was using both mm and MM. I thought I was using mm in both the cases.
    – Avinash
    Jun 29, 2016 at 19:35
  • It's a good document to bookmark. Or at least to remember how to look up.
    – Adrian Larson
    Jun 29, 2016 at 19:36
  • Yes...I'm going through that document now :)
    – Avinash
    Jun 29, 2016 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .