function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
K.G.K.G. 

Apex - Converting String To Date With Weird String Format

Hi Developer Forums,
I need to take a String of the format "July 9, 2015" and turn it into a Date object.

Apex doesn't seem to let me instantiate DateFormat / SimpleDateFormat objects, so I can't write my own format to use with a ".parse()" method.  The "9" and the "2015" are close enough to integers, so I can do some annoying string transformation to get my input to "Date.parse()" into the type of string Apex expects, but I really don't want to write 12 lines of code to turn spelled-out months into numbers.

Ordinarily in Java, I'd just try this:
DateFormat myFormat = new SimpleDateFormat('MMMM d, yyyy', Locale.ENGLISH);
Date myDate = myFormat.parse(myQuirkyDateString);
(via http://stackoverflow.com/a/11493647)

But that doesn't compile in Apex.

What can I do to turn this type of string into a Date?

Thanks!
-K.
Best Answer chosen by K.G.
Jason BealJason Beal
The date parsing methods of the Date class will not be able parse that date for you. The function below should do it. 

Date.parse - “Constructs a Date from a String. The format of the String depends on the local date format."
Date.valueOf - "The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone."

Apex function to parse a date in the format MMMM d, yyyy
Date parseLongDate(String dateString){
    Map <String, Integer> months = new Map <String, Integer> {'january'=>1, 'febuary'=>2
        , 'march'=>3, 'april'=>4, 'may'=>5, 'june'=>6, 'july'=>7, 'august'=>8, 'september'=>9
        , 'october'=>10, 'november'=>11, 'december'=>12};
    List <String> dateParts = dateString.toLowerCase().replace(',','').split(' ');
    Integer month = months.get(dateParts[0]);
    Integer day = Integer.valueOf(dateParts[1]);
    Integer year = Integer.valueOf(dateParts[2]);
    Date parsedDate = Date.newInstance(year,month,day);
    return parsedDate;
}


 

All Answers

Jason BealJason Beal
The date parsing methods of the Date class will not be able parse that date for you. The function below should do it. 

Date.parse - “Constructs a Date from a String. The format of the String depends on the local date format."
Date.valueOf - "The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone."

Apex function to parse a date in the format MMMM d, yyyy
Date parseLongDate(String dateString){
    Map <String, Integer> months = new Map <String, Integer> {'january'=>1, 'febuary'=>2
        , 'march'=>3, 'april'=>4, 'may'=>5, 'june'=>6, 'july'=>7, 'august'=>8, 'september'=>9
        , 'october'=>10, 'november'=>11, 'december'=>12};
    List <String> dateParts = dateString.toLowerCase().replace(',','').split(' ');
    Integer month = months.get(dateParts[0]);
    Integer day = Integer.valueOf(dateParts[1]);
    Integer year = Integer.valueOf(dateParts[2]);
    Date parsedDate = Date.newInstance(year,month,day);
    return parsedDate;
}


 
This was selected as the best answer
K.G.K.G.
Thanks @Jason Beal !  I already finished some very long code kind of like this, but yours is much cleaner.

If you happen to have any time, would you show me how you would convert the following data into a dateTime?
 
Integer knownYear = 2015;
String restOfDate = 'December 9, 5:00 PM';

(In case you're curious, these are separate because what I'm actually working with is String inputDate = 'Wednesday, December 9, 5:00 PM' without any year at all - just some business rules about "this year or next," so by the time I'm putting together the DateTime I'll already know the year from separate calculations.)

Thanks!
K.G.K.G.
(P.S.  I've done it a messy way already - just interested in cleaning up my code for legibility.  Really, no worries if you don't have time, but curious to see your style if you do.)
Jason BealJason Beal
Nothing too fancy just figure out what the delimiters are and split up the strings. How does this look.
 
Integer knownYear = 2015;
String restOfDate = 'December 9, 5:00 PM';

Map <String, Integer> months = new Map <String, Integer> {'january'=>1, 'febuary'=>2, 'march'=>3, 'april'=>4, 'may'=>5, 'june'=>6, 'july'=>7, 'august'=>8, 'september'=>9, 'october'=>10, 'november'=>11, 'december'=>12};
String[] dateTimeParts = restOfDate.split(', ');
String[] dateParts = dateTimeParts[0].split(' ');
String[] timeParts = dateTimeParts[1].split(' ');
Integer year = knownYear;
Integer month = months.get(dateParts[0].toLowerCase());
Integer day = Integer.valueOf(dateParts[1]);
Integer hour = Integer.valueOf(timeParts[0].split(':')[0]) + (timeParts[1] == 'PM' ? 12 : 0);
Integer minute = Integer.valueOf(timeParts[0].split(':')[1]);
Integer second = 0;
Datetime parsedDate = Datetime.newInstance(year, month, day, hour, minute, second);