I am getting different formats of date formats in date field from Json as a string.

I should be able to convert any Date format into Date based on user locale.below are the example of formats.


  1. "date": "2018-12-10T10:03:00.116Z"
  2. "date": "Mon Sep 17 10:35:28 2018"

I implemented below code to convert it.

string Dt = (string)sysdet.get('date');                          //getting string values of date from json content in msg field                  
    //if(String.isNotBlank(Dt) &&  
    //Pattern.matches('\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d', Dt))
    String[] str = dt.split('T');                    
    String JDate =string.valueof(str[0]);
    /*  string[] dts=JDate.split('-');
string dd=string.valueof(dts[2]);
string mm=string.valueof(dts[1]);      
string yyyy=string.valueof(dts[0]);
string finaldate=mm+'/'+ dd+'/'+ yyyy ;      */       
    Date parsedate= Date.valueof(JDate); 
    date datval=system.today();
    DateTime dtNow =parsedate; 
    String formattedDate = dtNow .format('yyyy-MM-dd',  UserInfo.getTimeZone().toString());
    system.debug('formattedDate2:::' +formattedDate );
    //date dateval=system.today();
    Licensedate=date.valueof(formattedDate );
    // system.debug('finaldate::'+finaldate);
if( Licensedate !=null)
    DateTime dtValue = DateTime.newInstance(Licensedate.year(), Licensedate.month(), Licensedate.day());
    FormattedDate = dtValue.format('dd-MMM-yyyy');                                              
    system.debug('FormattedDate ::'+FormattedDate );

Is there any method that convert string of any date format to standard format of date?

  • 1
    You shouldn't be getting user locale strings in JSON for dates. If possible, consider fixing the client software to use the ISO-8601 date/time standard.
    – sfdcfox
    Dec 12, 2018 at 14:21

2 Answers 2


Salesforce does not provide services to parse arbitrary inbound date-time formats. (Note that the values you're quoting are date-times, not dates).

You can parse these values in a number of ways, but there's no single method that accepts a date-time and a format string against which to parse it.

JSON Deserialization

DateTime incomingDateTime = JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);

This approach can handle ISO-format dates, as shown above. Note the extra set of double quotes, which make it into a JSON value for the parser's purposes.

This is applicable to your first example,

"date": "2018-12-10T10:03:00.116Z"

Because this date-time format is both comprehensive and well-standardized, it's preferable to use it wherever possible.

DateTime.valueOf() and Date.valueOf()

DateTime.valueOf() and Date.valueOf accept a string in a standard format similar to but not quite identical to that handled by the JSON parser:

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

Note that it uses the local time zone, where the value above specifies "Z" for GMT.



Constructs a Datetime from the given String in the local time zone and in the format of the user locale.

This method is best used for processing user input (if at all), because it's tied to the local time zone and the locale of the running user (not the organization). You can't specify a locale for parsing.

Manual Parsing

As you've started to do in your code, you can implement parsers for other non-standard date and time formats yourself. The logic for these is usually pretty straightforward (using split() and possibly some regexes) but you'll have to be careful to make sure you handle time zones appropriately.

Converting DateTime to Date

You can call the date() and dateGMT() methods on any DateTime value to get its associated Date. You must be aware of time zones, though: Date-Time values have a time zone, and Dates do not. The time zone in which the Date-Time is interpreted at the time of conversion can affect what Date is returned.

  • Thanks for Quick reply @David .I am getting the above two date formats in Json as string. Ishould able to get date only from those formats.I used parse() method and it worked fine. but i was getting datetime. I just need date only.Is there any snippet or code for getting yyyy,mm,dd from any format of date string other than manual parsing?
    – chandu
    Dec 13, 2018 at 9:07
  • Sure, you can convert a DateTime to a Date. See extended answer.
    – David Reed
    Dec 13, 2018 at 13:57

Use method for that:


Returns the Date as a string using the locale of the context user

Here are what those formats would look like:

// In American-English locale
date myDate = date.newInstance(2001, 3, 21);
String dayString = myDate.format();
system.assertEquals('3/21/2001', dayString);
  • That's the inverse of what chandu is trying to do - he has a string and wants to parse it.
    – David Reed
    Dec 12, 2018 at 14:23
  • @DavidReed Ok and what's wrong with this method that can not use it? Explain
    – Adamo
    Dec 13, 2018 at 14:06
  • 1
    I didn't say it's wrong. It just doesn't do what chandu wants. He has a String and wants a Date. You are explaining how to take a Date and make a String.
    – David Reed
    Dec 13, 2018 at 14:11
  • Ok, understood.
    – Adamo
    Dec 13, 2018 at 14:15

You must log in to answer this question.

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