I have a String "Saturday, Oct, 25th, 11:40"
What format does this date has? How can I parse the ordinal indicator?
Here is how i want to convert it
private String changeDateFormat(String stringDate){
DateFormat dateFormat = new SimpleDateFormat("DD, MM, ddth, hh:mm");
try {
Date date = dateFormat.parse(stringDate);
dateFormat = new SimpleDateFormat("ddMMMyyyy");
stringDate=dateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return stringDate.toUpperCase();
}
One solution would be to parse the parameter in order to know which pattern to use:
This might need some optimisation, but it gives the idea.
Hope this program solve your problem.
SimpleDateFormat
doesn't seem to easily handle day numbers followed by "st" "th" etc. A simple solution would be remove that part of the original string. E.g.After that just use this format on
stringDate
:The java doc has lot of information on how to parse a date from String in different formats :
SimpleDateFormat Java Doc
You can try with this, but play around this and refer java doc until you are able to solve your problem :
Try different combinations, that way you will learn more about SimpleDateFormat and how it works with different formats.