You need to sign in to do that
Don't have an account?
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:
But that doesn't compile in Apex.
What can I do to turn this type of string into a Date?
Thanks!
-K.
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.
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
All Answers
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
If you happen to have any time, would you show me how you would convert the following data into a dateTime?
(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!