SimpleDateFormat Java - java

I need the DATE_FORMAT for this case: "Thu Feb 17 08:50:02 UTC 2022"
The example code is this:
date = new SimpleDateFormat(DATE_FORMAT).parse(s);
where DATE_FORMAT is the format that I need to find and "s" is String "Thu Feb 17 08:50:02 UTC 2022"
Can someone help me ?

This should work for you:
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse("Thu Feb 17 08:50:02 UTC 2022");

Related

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

Set a date object in java from a string retaining the same format

I have an application where the data is read from the database. Database has a date column value stored in the format
Sat Oct 19 10:03:00 EDT 2013
Then I read database from my java code using getters and setters where the date column is
node.setHeadTime(Date newDate)
node.getHeadTime();
Hence Its a date object I get the date as
Sat Oct 19 19:33:00 IST 2013
Now I do some processing wherein I need to update the data and store it back to the database.
The data is changed to
Sat Oct 19 19:35:00 IST 2013
I need to store back this data to the database as
Sat Oct 19 10:05:00 EDT 2013
I tried the following code:
DateFormat edtFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
TimeZone edt = TimeZone.getTimeZone("America/New_York");
edtFormat.setTimeZone(edt);
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy"); // The timezone 'z' identifier was missing, I have added that now.
Date dd1 = df.parse(headDate); //headdate is the date having string Sat Oct 19 19:35:00 IST 2013
String hd=edtFormat.format(dd1);
log.info("date "+hd);
fnFillDetails(String hd);
this prints Sat Oct 19 10:05:00 EDT 2013 successfully.
But my issue is
After I change the format I am sending it to a function
fnFillDetails(String hd){
node.setheaddate(hd); // But this obviously has an error as type mismatch of Date and String.
}
I tried all possible workarounds none worked. I cannot change the datatype of the headdate to String.
How do I fix this issue? I need to retain the format and change it back to orignal format in the database.
String headDate = "Sat Oct 19 19:35:00 IST 2013";
DateFormat edtFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
TimeZone edt = TimeZone.getTimeZone("America/New_York");
edtFormat.setTimeZone(edt);
DateFormat istFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
TimeZone ist = TimeZone.getTimeZone("Asia/Kolkata");
istFormat.setTimeZone(ist);
Date dd1 = istFormat.parse(headDate);
String hd=edtFormat.format(dd1);
System.out.println(hd);
So now if you want to pass edt format to fnFillDetails
then call
Date edtDate = edtFormat.parse(hd);
fnFillDetails(edtDate);

Parse time with SimapleDateFormat

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.

how to convert date in java

I am new in android .
I have to convert following date into this time stamp (Wed Oct 12 14:17:42 GMT+05:30 2011)
Thu, 27 May 2010 12:37:27 GMT
This is the date that I am getting from the server through the header. I have converted it into the String object. Now I have to convert it into the Date format like: Wed Oct 12 14:17:42 GMT+05:30 2011
Please could you help me how should I convert it into the (Wed Oct 12 14:17:42 GMT+05:30 2011) this format using timestamp.
Have a look at DateFormat or SimpleDateFormat which provide parse() and format() methods. DateFormat provides a bunch of standard formats whereas SimpleDateFormat allows you to provide your own format expression.
Example for your input date:
//note that you need the locale as well, since the weekdays and months are written in a specific language, English in that case
SimpleDateFormat parseFormat = new SimpleDateFormat( "E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH );
SimpleDateFormat writeFormat = new SimpleDateFormat( "E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH );
writeFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
Date tDate = parseFormat.parse( "Wed, 12 Oct 2011 14:17:42 GMT" );
System.out.println(writeFormat.format(tDate )); //Wed Oct 12 14:17:42 GMT 2011
Edit: If you want a more usable API, try JodaTime.

Categories