Parsing date format with timezone [duplicate] - java

This question already has answers here:
Java SimpleDateFormat Pattern for JavaScript Date
(4 answers)
Closed 5 years ago.
I'm trying to parse a date string with a GMT+100
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss").parse("Thu Apr 23 2015 11:30:49 GMT+0100")
Comes out as
Thu Apr 23 11:30:49 UTC 2015
If I add z or Z or X to the format, it's unparseable. If I don't add it, it's off by the offset, one hour.
What is the right way to parse this date?
--
Update: this differs from Java SimpleDateFormat Pattern for JavaScript Date in two ways: 1) this question is pure java and would have accepted answers other than those using SimpleDateFormat (i.e. new Java 8 features), and 2) the solution here is different to the other question.

Your expresssion should be EEE MMM dd yyyy HH:mm:ss 'GMT'Z(including the quotes), so your code is as follows:
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z").parse("Thu Apr 23 2015 11:30:49 GMT+0100")
HOW THIS WORKS
According to the docs, anything passed in between single quotes(') in SimpleDateFormat pattern is not interpolated, but is assumed be just a part of the date format to be ignored while parsing.
And the equivalent of +0100 in SimpleDateFormat pattern terms is Z

Related

Simple date format parses 2k11 like values which should throw an exception [duplicate]

This question already has answers here:
SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?
(7 answers)
Why java SimpleDateFormat can parse formatted text with extra characters?
(4 answers)
Closed 3 years ago.
I am trying to validate dates using simple data format and it parses strange dates.
val dateFormat = new SimpleDateFormat("dd MMM, yyyy")
dateFormat.setLenient(false)
dateFormat.parse("01 Jan, 2k11")
Sun Jan 01 00:00:00 IST 2
fast time : -62104253400000
cDate : C.E. 2-01-01T00:00:00.000+0530
I started digging through the source code for SimpleDateFormat, and read enough to glean what appears to be happening here. What is happening is that your year component 2k11 is being interpreted as a two digit year (actually one digit, 2), and everything that follows is being ignored as not being part of the date pattern. Consider the following code which produces the same result:
DateFormat dateFormat = new SimpleDateFormat("dd MMM, yyyy");
dateFormat.setLenient(false);
Date blah = dateFormat.parse("01 Jan, 2 Jon Skeet");
System.out.println(blah);
This prints:
Sun Jan 01 00:00:00 CET 2
As you can see, the intepreted year is 2, and the Jon Skeet blurb was ignored. To understand exactly why SimpleDateFormat is doing this, read the source code.

Convert Date in Java [duplicate]

This question already has answers here:
How to convert getTime() to 'YYYY-MM-DD HH24:MI:SS' [duplicate]
(3 answers)
Java string to date conversion
(17 answers)
Closed 4 years ago.
I have a date like "Sun Apr 01 01:00:00 EEST 2018" and I want to convert it to the following numeric format: Day-Month-Year Hours:Minutes:Seconds
I want to do that in Java. Can anybody help?
Thank you a lot
I would use the DateTimeFormatter for this. You can find more information in the docs.
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
However, here an example:
DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
LocalDateTime date = LocalDateTime.parse("Sun Apr 01 01:00:00 EEST 2018", parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date)); //01-04-2018 01:00:00
The Locale.US part is required even if you don't live in the US, otherwise it may not be able to parse Sun to Sunday and Apr to April an so on. Probably it works with Locale.UK to and more but for example it didn't work for me without because i live in switzerland.

Confused with Converting the date with SimpleDateFormat [duplicate]

This question already has answers here:
How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012?
(5 answers)
Closed 5 years ago.
I have a problem with date converting. I use the following program and I expect the output: 19.05.2017
But the output is: 05.00.2017
Can anybody help?
String t = "Fri May 19 00:00:00 CEST 2017";
Date d = new SimpleDateFormat("EEE MMM DD hh:mm:ss zzzz YYYY", Locale.US).parse(t);
String s = new SimpleDateFormat("dd.mm.yyyy").format(d).toString();
System.out.println(s);
A surprising result. The oldfashioned classes SimpleDateFormat and friends are full of surprises. This is meant as a negative thing.
Uppercase DD is day of year. Lowercase hh is hour of AM or PM (1 through 12). Uppercase YYYY is weekbased year (only useful with week number). So you are asking for a date that is a Friday in May and the 19th day of the year. Obviously this is not possible.
The result of parsing is Thu Jan 05 23:00:00 CET 2017. Apparently SimpleDateFormat opts for giving you a Friday and for using the zone offset of 2 hours implied by CEST even though the date it has chosen is not at the time of year where CEST (summer time) is in use. I don’t know whether it just gives you the first Friday of the weekbased year (Friday in week 1 of the year). Friday at 0000 hours at offset GMT+2 equals Thursday at 23 at GMT+1, which is CET.
Next for the formatting, 05 is the date as expected, but lowercase mm means minutes. Since the minutes are 0, you get 00. You got the right year.
Rather than using the outdated classes that give you such surprises, I agree with Sam’s answer that you should use the newer classes in java.time:
ZonedDateTime dt = ZonedDateTime.parse(t,
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US));
String s = dt.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));
This code gives you 19.05.2017 as you had expected. One of the good things about the modern classes is, if you try to parse with your original format pattern string, you will get a DateTimeParseException so you will know something is wrong. I certainly prefer an exception over incorrect output.
Another good thing is these classes respect the time zone in the input and use it in the output too (unless you explicitly instruct them otherwise). They will never turn Friday 6 January into Thursday 5 January because of some funny time zone issue.
Your input date is in Central European Summer Time and your date format is a bit wrong. Try
SimpleDateFormat input = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy");
You might want to set the timezone on the output date format in order to get the date in the correct local time.
Ideally you'd move over to use a java.time style as shown here:
https://www.mkyong.com/java/java-convert-date-and-time-between-timezone/

java Date to sencha Date using dateFormatter [duplicate]

This question already has answers here:
Date and time conversion to some other Timezone in java
(11 answers)
Closed 8 years ago.
I need Date strings sent to sencha frontend of the form:
'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
I'm unable to get the right text formatter string to output in this form.
Has anyone converted a Java Date object to this format ?
PS: not a duplicate of time lag while converting between timezones,
Its a query to get Dates in a specific format required for frontends using sencha which isn't answered by the "dd-MM-yyyy HH:mm:ss" format used in this post
This is the format pattern you need:
"EEE MMM dd yyyy HH:mm:ss zZ (zzzzzzz)"

Java string convert to Date

I need to change this format to Date
Fri Oct 28 05:47:54 SGT 2011
I not sure the SGT how to set?
You can find all patterns in the SimpleDateFormat javadoc. SGT is a general time zone and it's listed as z. So, the pattern should look like:
EEE MMM dd HH:mm:ss z yyyy
The month names seems to be English, so this should do in combination with Locale.ENGLISH:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date date = sdf.parse(string);
// ...
Interesting enough, that's also basically the default pattern of the Date#toString() method (which you would see when you're doing a System.out.println(date), for example).
Take a look at the DateFormat class, specifically the parse method.

Categories

Resources