Parse time with SimapleDateFormat - java

I'm trying to parse date like this: Tue Aug 28 21:16:23 +0000 2012 with this code:
SimpleDateFormat format = new SimpleDateFormat("E M dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = object.getString("created_at"); // d = Tue Aug 28 21:16:23 +0000 2012;
date = format.parse(d);
But there is exception:
09-28 11:10:24.471: W/System.err(10388): java.text.ParseException: Unparseable date: "Fri Sep 28 07:09:09 +0000 2012" (at offset 4)
Where I make a mistake?

try this
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
MMM is used to represent short Month.

you need MMM for month representation for Aug.
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ssZ yyyy", Locale.ENGLISH);
String d = "Tue Aug 28 21:16:23 +0000 2012"; // d =;
Date date = format.parse(d);
System.out.println(date);
Output:Tue Aug 28 22:16:23 BST 2012
It might help to look into SimpleDateFormat's javadoc, there are some helpful examples for pattern strings.

Related

Converting date object to yyyy-MM-dd hh:mm:ss [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
SimpleDateFormatter.parse giving output in different format than specified
(1 answer)
Closed 4 years ago.
I am trying to convert this date object Fri Sep 21 08:00:00 SGT 2018 to this format yyyy-MM-dd hh:mm:ss date object.
Date dt = sd1.parse(startTime);
logger.info(dt);
logger.info(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dt.toString()));
The first log statement works, it returns me Fri Sep 21 08:00:00 SGT 2018 but the second log statement does not work.
It throws me an error
unparseable date Fri Sep 21 08:00:00 SGT 2018
What am i doing wrong here? My end goal is to get the date object in yyyy-MM-dd hh:mm:ss format.
First you have to parse the date properly with :
new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
So this should work:
Date newDate = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse("Fri Sep 21 08:00:00 SGT 2018");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String strDate = sdf.format(newDate.getTime());
System.out.println(strDate);
But I suggest you to go with LocalDateTime
LocalDateTime ldt = LocalDateTime.parse("Fri Sep 21 08:00:00 SGT 2018", DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(ldt.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SS")));

SimpleDateFormat throws parse Exception for +0100

I am trying with two sets of date with date format :
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
It works fine for the Date : Fri, 26 Aug 2016 13:55:34 +0000
Not for the Date : Tue, 06 Sep 2016 11:57:14 +0100
Throws exception for the +0100 date.
Unparseable date: "Tue, 06 Sep 2016 11:57:14 +0100" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:555)
It fails at offset 0, which means that the problem is not related to the timezone but to the day in letters.
You should set the Locale of your SimpleDateFormat.
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
Date d1 = format.parse("Fri, 26 Aug 2016 13:55:34 +0000");
Date d2 = format.parse("Tue, 06 Sep 2016 11:57:14 +0100");
Works without any problem.
If you also need to retrieve the timezone, you will also have to add z to your pattern:
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
You need
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Note the z for the time zone.
The parser ignores the zero (+0000) case if z is not supplied, but not a non-zero (+0100) case. The lenient property controls this behaviour (Acknowledge #Marko Topolnik).
Since you're using English week names, you ought to use the two-argument constructor to SimpleDateFormat, passing Locale.ENGLISH as the second parameter.

SimpleDateFormatter Unparseable date exception

I have a string date Wed, 30 Mar 2016 01:39:56 +0000 which i want to convert to the date format "yyyy-MM-dd"
Below are the lines of code i am trying to achieve it. But it is returning unparseable date exception
String date_s = "Wed, 30 Mar 2016 01:39:56 +0000";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("MMM d, yyyy");
System.out.println(dt1.format(date));
Any advice?
Since Java 8, you can do it in one line with the java.time library.
LocalDateTime.parse("Wed, 30 Mar 2016 01:39:56 +0000", DateTimeFormatter.RFC_1123_DATE_TIME)
.format(DateTimeFormatter.ISO_LOCAL_DATE);
'Wed, 30 Mar 2016 01:39:56 +0000' is not in the right format for yyyy-MM-dd to parse it
If you want it to work, try this:
SimpleDateFormat in = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
Date date = in.parse("Wed, 30 Mar 2016 01:01:01 +0000");
String outdate = out.format(date);
System.out.println(outdate);
Ideone

How to handle the Time data from the Style just like "Wed Jun 20 11:01:05 +0800 2012"?

In my project ,I get json data from Server,there's a field named 'creat_at' in the json which style is like 'Wed Jun 20 11:01:05 +0800 2012'
How to change it to more easy-to-read style like '2012/06/20'?
(I have tried 'DateFormat.parse' but it dose not work:
new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString)
cause
java.text.ParseException: Unparseable date: "Wed Jun 20 11:00:53 +0800 2012")
See SimpleDateFormat API
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString);
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
You need to try SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(sdf.parse("Wed Jun 20 11:01:05 +0800 2012")));
prints
2012/06/20
You may also need to set an approriate timezone.

convert string into java.util.date format in java

am having a string like this.
Thu Oct 07 11:31:50 IST 2010
I want to convert this into its exact date time format to store it in SQL.
Am familiar with so many string to date conversions like the following.
String dateString = "2001/03/09";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
But i need to convert a string like Thu Oct 07 11:31:50 IST 2010
into its Date format with timestamp.
Can anyone explain the proper way of converting this into its java.util.Date format.?
Try this:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
For future reference read up on the SimpleDateFormat class:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use this format -'EEE MMM dd HH:mm:ss z yyyy'
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
.parse("Thu Oct 07 11:31:50 IST 2010");
System.out.println(date);
Can't you do like below
String str = "Thu Oct 07 11:31:50 IST 2010";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd hh:mm:ss 'IST' yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(sdf.parse(str)));

Categories