I have problem formating date.
From : EEE, d MMM yyyy HH:mm:ss Z (example : Mon, 05 Jan 2014 15:10:00 +0200)
To : dd/MMM/yyyy HH:mm (example : 05/01/2014 15:10)
Here is what i tried :
private String formatDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("dd/MMM/yyyy HH:mm");
Date dateResult = null;
try {
dateResult = format.parse(date);
}
catch (java.text.ParseException e) {
Log.e(TAG, "", e);
}
return dateResult.toString();
}
I get exception : unparseable date at offset 0
some help would be nice here thanks ;)
Try code below:
Try this
You need two times converting. For example:
For my own RSS parser I use the following code to parse different date formats:
Incorrect sample data
Your example data of
Mon, 05 Jan 2014 15:10:00 +0200
is incorrect. That date was a Sunday, not a Monday.java.time
The modern way is with the java.time classes.
Your input has a format that conforms with RFC 1123. The java.time classes include a formatter for that specific format.
See live code in IdeOne.com.
As for generating a string in other formats, that is already covered many times in Stack Overflow. Search for
DateTimeFormatter
class.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql.* classes.
Where to obtain the java.time classes?