Reputation: 369
In my project, I am trying to parse date format like this "Mon Oct 20 00:00:00 GMT+06:30 2014" to dd-MM-yyyy but I got the following error. I am hoping someone to solve me this problem.
Thanks,
10-20 13:03:01.390: W/System.err(23409): java.text.ParseException: Unparseable date: "Mon Oct 20 00:00:00 GMT+06:30 2014" (at offset 0)
parseDate.java
SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
String sdate="Mon Oct 20 00:00:00 GMT+06:30 2014";
try {
Date _date= formatter_date.parse(sdate);
holder.txtDate.setText(String.valueOf(_date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Upvotes: 5
Views: 24467
Reputation: 79055
Unfortunately, the accepted answer has missed a crucial thing, Locale
while parsing the date-time string. Therefore, that code will work merely by coincidence - only when the JVM on which it is executed, has an English Locale
set. Check Always specify a Locale with a date-time formatter for custom formats to learn more about it.
java.time
In March 2014, Java 8 introduced the modern, java.time
date-time API which supplanted the error-prone legacy java.util
date-time API. Any new code should use the java.time
API.
Your date-time string has a time zone offset. So, parse your date-time string into an OffsetDateTime
using DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu", Locale.ENGLISH)
and format it using DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ENGLISH)
.
Demo:
public class Main {
private static final DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu",
Locale.ENGLISH);
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu",
Locale.ENGLISH);
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("Mon Oct 20 00:00:00 GMT+06:30 2014", parser);
System.out.println(odt);
// Formatted output
System.out.println(odt.format(formatter));
}
}
Output:
2014-10-20T00:00+06:30
20-10-2014
Note: For whatever reason, if you need an instance of java.util.Date
from this object of OffsetDateTime
, you can do so as follows:
java.util.Date date = Date.from(odt.toInstant());
Learn more about the modern date-time API from Trail: Date Time.
Upvotes: 2
Reputation: 1325
Replace
SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
with
SimpleDateFormat formatter_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Upvotes: 4
Reputation: 2877
use this code
public static String parseTodaysDate(String time) {
String inputPattern = "EEE MMM d HH:mm:ss zzz yyyy";
String outputPattern = "dd-MM-yyyy";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = null;
String str = null;
try {
date = inputFormat.parse(time);
str = outputFormat.format(date);
Log.i("mini", "Converted Date Today:" + str);
} catch (ParseException e) {
e.printStackTrace();
}
return str;
}
Upvotes: 8