Parse date in android - java

I have the following code to pase a date
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
Date _pubDate = df.parse(_pubDateE.getFirstChild().getNodeValue());
But I get this error:
java.text.ParseException: Unparseable date: "Fri, 12 Aug 2011 15:34:47 CEST"
What is wrong ?

You're missing the timezone in the date format at the end, in your exception message, the "CEST" part.
Your code
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
should be
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z", Locale.getDefault());
You might want to read SimpleDateFormat
Edit
At the bottom of this page, the timezone format is more cleary explained
Clearer Timezone format

I think you need to add zzz in the end (for timezone):
"EEE, dd MMM yyyy kk:mm:ss zzz"

The date string is not in the format you specified. Notice the time zone at the end?

You probably want: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());

Related

Talend - Transform "EEE MMM dd hh:mm:ss z yyyy" in "yyyy-MM-dd"

I receive a date from a file in this format: "EEE MMM dd hh:mm:ss z yyyy" and I'm trying to convert this value into Date "yyyy-MM-dd". For that I'm using:
TalendDate.parseDate("yyyy/MM/dd", TalendDate.formatDate("yyyy/MM/dd", TalendDate.parseDate("EEE MMM dd hh:mm:ss z yyyy",context.date)))
The context.date is defined here:
context.date = input_row.mtime_string;
But when I run my JavaRow component I get the following error:
Exception in component tJavaRow_1
"java.lang.RuntimeException: java.text.ParseException: Unparseable date: "Thu Aug 09 10:38:45 BST 2018"
How can I solve this?
Many Thanks!
You could achieve the format using the below code snippet -
System.out.println(input_row.newColumn);
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse(input_row.newColumn);
String dDate = null;
parserSDF = new SimpleDateFormat("yyyy-MM-dd");
dDate = parserSDF.format(date);
System.out.println(dDate);
Also, you need the below libraries to be imported(Advanced settings section of tJavaRow)-
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;
I had directly passed the input value from file(in my scenario tFileInputDelimited) into tJavaRow as - input_row.newColumn and then used SimpleDateFormat class to both parse and format dates according to the formatting pattern.
Read more here.
If you want to convert the date into LocalDate then below code might help:
private LocalDate getLocalDate(String date){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss z yyyy", Locale.getDefault());
return LocalDate.parse(date, formatter);
}
And once you get the LocalDate, you can transform it to any format. As the question expects yyyy-MM-dd then just call toString() on LocalDate object.
LocalDate curr = LocalDate.now();
System.out.println(curr.toString());
It will display the date like "2019-11-20".
Hope this helps to someone.

Android can't parse string to date

I have this string
Wed, 08 Jan 2014 9:30 am WET
and needed to be parsed to a Date object, I tried lot of masks but didn't work, here is the last thing I tried that I thought it would work with but didn't
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa z", Locale.ENGLISH);
thanks
stack trace
01-08 14:25:25.906: W/System.err(13288): java.text.ParseException: Unparseable date: "Wed, 08 Jan 2014 11:59 am WET"
01-08 14:25:25.914: W/System.err(13288): at java.text.DateFormat.parse(DateFormat.java:626)
I ended up using this instead
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa", Locale.ENGLISH);
Date date = dateFormat.parse(dateString.substring(0, dateString.length() - 4));
that WET part was the cause so I removed it, it wouldn't give the exact time but I only need the day and month,
Give a Locale to your Formatter where days and months are in English, otherwise it will use your default locale (that I presume is not English) and hence can't parse your String.
SimpleDateFormat dateFormat =
new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa z", Locale.ENGLISH);

How to parse date to EEE MMM dd HH:mm:ss zzz yyyy format?

I am getting date from database as 2013-05-03 00:20:29.0. I want to parse the date to EEE MMM dd HH:mm:ss zzz yyyy format, but when I am parsing it, I am getting this exception:
java.text.ParseException: Unparseable date: "2013-05-03 00:20:29.0"
My code is below:
String createdDate = "2013-05-03 00:20:29.0";
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parseDate = format.parse(createdDate);
You have to adjust the Format pattern. Format must be yyyy-MM-dd HH:mm:ss.S
Try this
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Your SimpleDateFormat pattern does not match createdDate. The format should be:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
See the javadoc
As I understod you want to convert your date to a new format
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2013-05-03 00:20:29.0");
String str = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date);
You can also try to get the date from the DB not as String but as java.sql.Date then the first step will be unnecessary
Here is an example:
String str_date= "29/02/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date dt = sdf.parse(str_date);
DateFormat df = new SimpleDateFormat("EEEE");
System.out.println(df.format(dt));

SimpleDateFormat: unparseable date exception with correct pattern

I have simple code, it still throws me unparseable date exception. I'm confused, the pattern seems to be correct.
DateFormat dffrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
dffrom.parse("Sun Jan 20 00:50:24 CET 2013");
I tried also these patterns:
"EEE MMM dd HH:mm:ss z yyyy"
"EEE MMM dd HH:mm:ss z YYYY"
"EEE MMM dd HH:mm:ss zzz YYYY"
Your default Locale may not recognize the words "Sun" and/or "Jan". Try with ENGLISH Locale:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
To view your default Locale you can use:
System.out.println(Locale.getDefault());
See: Locale
This depends on your Locale.
DateFormat dffrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
will work.

Parse RSS pubDate to Date object in java

How can I parse a pubDate from a RSS feed to a Date object in java.
The format in the RSS feed:
Sat, 24 Apr 2010 14:01:00 GMT
What I have at the moment:
DateFormat dateFormat = DateFormat.getInstance();
Date pubDate = dateFormat.parse(item.getPubDate().getText());
But this code throws an ParseException with the message Unparseable date
You can define the date format you are trying to parse, using the class SimpleDateFormat:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");
Additionally, for non-English Locale's, be sure to use the following when parsing dates in English:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
If you need to have an RFC822 compliant date, try this :
DateFormat dateFormatterRssPubDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
For the lucky one that can use the Java 8 LocalDateTime:
LocalDateTime localDateTime = LocalDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Sat, 24 Apr 2010 14:01:00 GMT"));

Categories