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.
Ex:
- "date": "2018-12-10T10:03:00.116Z"
- "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(Dt.contains('T'))
//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('Licensedate::'+Licensedate);
system.debug('parsedate::'+parsedate);
system.debug('str::'+str);
// system.debug('finaldate::'+finaldate);
system.debug('JDate::'+JDate);
}
else
{
Licensedate=null;
}
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?