I have date in "Monday 29th June 2015" this format. And i want to change this date format in "2015-06-29" this format.
Asked
Active
Viewed 673 times
-1
-
Have a look at the class `SimpleDateFormat` – beresfordt Jun 29 '15 at 06:56
3 Answers
0
If you have a string with your date, that you want to convert, use something like this:
String dateString = "Monday 29th June 2015";
//the output format
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
//the input format of the original date
date = new SimpleDateFormat("EEEE dd MMMM yyyy")
.parse(dateString);
Log.e("result date", "Date:" + simple.format(date));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Gabriella Angelova
- 2,985
- 1
- 17
- 30
-
1this will give an error-> java.text.ParseException: Unparseable date: "Monday 2nd November 2015" (at offset 3) – Ashish Jaiswal Jun 29 '15 at 07:41
-
Then try to find out which is the exact pattern for "Monday 2nd November 2015" or just eliminate the "nd" (or "st", "th") part after the digit and try again with the pattern that I gave you – Gabriella Angelova Jun 29 '15 at 07:51
0
In Your Date "Monday 29th June 2015" th make it unparseable so i think you should remove that and try to parse your date
String dateString = "Monday 2nd June 2015";
String str[] = dateString.split(" ");
str[1] = str[1].replaceAll("rd","");
str[1] = str[1].replaceAll("th","");
str[1] = str[1].replaceAll("st","");
str[1] = str[1].replaceAll("nd","");
dateString = str[0]+" "+str[1]+" "+str[2]+" "+str[3];
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = new SimpleDateFormat("EEEE dd MMMM yyyy").parse(dateString);
System.out.println(" Date:" + simple.format(date));
} catch (Exception e) {
System.out.println(e.getMessage());
}
In my code i have take an assumption that you have string values for your date of and which contains 2 digits for all date of months like
"Monday 01st June 2015"
if it is not like that you have to convert your date value in 2 digit number OR change my parsing logic to remove st or rd etc from date of month
Hope it will help :)

Vishnu
- 375
- 4
- 16
-
this won't work if you have "Monday 2nd November 2015" for example, where the date is with only one digit. I think that the right way to eliminate the letters after the digit(s) is to go through the string (for example "2nd") and get all parsable digits – Gabriella Angelova Jun 29 '15 at 07:56
-
-
-1
Something like,
Date d = new Date(old);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date=sdf.format(d);

Daud Arfin
- 2,499
- 1
- 18
- 37