I need to parse dates like Thursday, May 29, 2008 1:45 PM for a current project and don’t have much time get it done. I realize I can write some custom parser but that would take a while, I’ve tried a few date parsers I’ve found but none are working for this, I would greatly appreciate if anyone has any advice. I mainly just need to capture the month, day and year as integers, like:
int month = 5;
int date = 29;
int year = 2008
thanks for any advice
Use
SimpleDateFormat. According to the docs, the pattern of your date string would be"EEEE, MMMM d, yyyy h:mm a". You can create aSimpleDateFormatobject using this pattern and use it to parse strings:As BalusC states in the comments, if your code will be used on machines that do not have English set as the primary language, the above snippet will fail. To avoid this, specify English explicitly when constructing the parser.
To pull individual values out of the date object, you’ll need to construct a
Calendar:You can then ask it for the values you want.