Month is always January at date parsing [duplicate] - java

This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 3 years ago.
i tried to convert a date String into a date object, but unfortunately the month is set every time to Jan and I can't figure out why.
String date = "20190522T072424.000Z";
DateFormat sdf = new SimpleDateFormat("yyyyMMDD'T'HHmmss.SSS'Z'");
System.out.println(sdf.parse(date));
Output: Tue Jan 22 07:24:24 CET 2019

As AxelH said, the format DD is for day of the year. If you use yyyyMMdd'T'HHmmss.SSS'Z' you will print Wed May 22 07:24:24 WEST 2019.
For reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

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.

Invalid date is populated when we use yyyy-MM-dd'T'HH:mm:ssXXX format in java [duplicate]

This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
java.util.Date is generating a wrong date?
(3 answers)
Closed 3 years ago.
When we convert the date from yyyy-MM-dd'T'HH:mm:ssXXX to YYMMDD date is invalid.
Say..
If the date is 2019-02-27T12:52:58.249Z
then the converted date is generated as "190258"
The issue is because of 'D' in the input format.
D represents Day of the year - so when we give 2019-02-27 it adds the 31 days in Jan and 27 days in Feb so the day is counted as '58'.
After changing the format to 'd' it works fine.
Similarly, 'Y' represents week of the year - so we have replaced that to 'y' which represents year

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.

SimpleDateFormat not giving correct date [duplicate]

This question already has answers here:
java date format yyyy-mm-dd.hh.MM.ss.ms
(3 answers)
Closed 5 years ago.
I am trying to parse the date from a given String. My input string is 2017-08-11, which represents Aug 11th 2017 (Basically yyyy-mm-dd format)
However if I run
new SimpleDateFormat("yyyy-mm-dd").parse("2017-08-11")
I get Wed Jan 11 00:08:00 GMT 2017
Why is this? I have created a test program at: http://ideone.com/dHe0ZA
M (capital M) represents month and m (small m) represents minute in java.
So the format you have specified is wrong.
Try
new SimpleDateFormat("yyyy-MM-dd").parse("2017-08-11")
You can see more details about the formats here.

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/

Categories

Resources