I am getting an unexpected output of SimpleDateFormat.parse(). - java

When I am executing the following code
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/YYYY");
Date today=new Date();
String olddate="03/11/2016";
System.out.println(sdf.parse(olddate));
Why the Result is displaying Sun Dec 27 00:00:00 IST 2015.
However I was expecting Wed Nov 03 00:00:00 IST 2016.
Anyone can please explain. And how can I get the desired output.

Date format is wrong. It should be y instead of Y-
dd/MM/yyyy
Y specify Week year
y specify Year
For a more detailed insight view, check the link Oracle Docs and you'll see the meaning of each character we pass there.

Related

Unparseable Date In America/Mazatlan timezone

I'm on America/Los_Angeles TZ, and when I try to render midnight in the America/Mazatlan TZ, I get the following exception:
Exception in thread "main" java.text.ParseException: Unparseable date: "12:00 AM"
Here is my code to reproduce this:
DateFormat dateFormat = new SimpleDateFormat("h:mm a");
TimeZone timeZone = TimeZone.getTimeZone("America/Mazatlan");
dateFormat.setTimeZone(timeZone);
dateFormat.setLenient(false);
Date parse = dateFormat.parse("12:00 AM");
I'm aware the commenting out the setLenient(false) will fix the issue, I'm just not sure why that is a fix as other timezones in the same offset, such as America/Inuvik do not cause issues like this.
Any assistance would be great.
When you don't specify a date, 1970-01-01 is used.
The time zone definition for Mazatlan shows that the base offset switched from -08:00 to -07:00 in 1970. This creates a discontinuity in local time, similar to the kind usually found during a spring-forward daylight saving time transition.
There is an hour of missing local time, from midnight to just before 1:00. Times in this range are invalid. Assuming the zone definition is correct, that means the clocks ticked forward like this:
======== UTC ======= ==== America/Mazatlan ===
1970-01-01T07:59:57Z 1969-12-31T23:59:57-08:00
1970-01-01T07:59:58Z 1969-12-31T23:59:58-08:00
1970-01-01T07:59:59Z 1969-12-31T23:59:59-08:00
1970-01-01T08:00:00Z 1970-01-01T01:00:00-07:00 (transition!)
1970-01-01T08:00:01Z 1970-01-01T01:00:01-07:00
1970-01-01T08:00:02Z 1970-01-01T01:00:02-07:00
Therefore, if you are using SimpleDateFormat - you should include a date, not just a time.
If you remove the line,
dateFormat.setLenient(false);
Your parse object value is becoming
Thu Jan 01 10:00:00 EET 1970
I don't know why but for America/Mazatlan TZ this line is creating exception.
For America/Los_Angeles TZ and America/Inuvik TZ, usage of dateFormat.setLenient(false) line not giving any error and results are the same with America/Mazatlan TZ.
Thu Jan 01 10:00:00 EET 1970
That because you dateFormat.setLenient(false); and 12:00 should be 'PM' not 'AM'

how to check different date formats in java

I am getting different date formats below dd-MM-yyyy,dd/MM/yyyy,yyyy-MM-dd,yyyy/MM/dd
SimpleDateFormat sm1 = new SimpleDateFormat("dd-MM-yyyy");
String date = "01-12-2013";
System.out.println("Date 1 is "+sm1.parse(date));
date = "2013-12-01";
System.out.println("Date 1 is "+sm1.parse(date));
the same simple date format gives the below result eventhough date format is wrong(ie:-2013-12-01).Below the results.
Date 1 is Sun Dec 01 00:00:00 IST 2013
Date 1 is Sun Jun 05 00:00:00 IST 7
You need to setLenient(false) to make parse() method throw ParseException for unparsable case
I have tried Jigar Joshi's answer.
==========================code=======================================
SimpleDateFormat sm1 = new SimpleDateFormat("dd-MM-yyyy");
sm1.setLenient(false);
String date = "01-12-2013";
System.out.println("Date 1 is "+sm1.parse(date));
date = "2013-12-01";
System.out.println("Date 1 is "+sm1.parse(date));
=========================Result========================================
Date 1 is Sun Dec 01 00:00:00 CST 2013
Exception in thread "main" java.text.ParseException: Unparseable date: "2013-12-01"
at java.text.DateFormat.parse(DateFormat.java:337)
at workflow.Test.main(Test.java:14)
Your date format is dd-MM-yyyy. That means the parser is expecting some day, month, and year format.
From the SimpleDateFormat documentation: the number of pattern letters in a Number type formatter is the minimum. So, while 2013 wouldn't make sense in our mind, it fits within the formatter's bounds.
You have provided 2013-12-01 as to fit into that format. What it appears the formatter is doing is providing December 1 (insert timezone here), and then adding 2,013 days to it.
That turns out to be June 6, 7 AD. There's some wiggle room for your timezone (I'm not sure which of the five timezones IST represents is actually yours).
So, believe it or not...the formatter is correct. Be very careful as to what kind of format you specify or allow in your dates!
If you don't want that parsed, then specify setLenient(false) on your instance of sm1.

java.util.Date.parse() throws an exception when parsing Wed Apr 03 00:00:00 BST 2013

Why is it throwing an exception, That date is pretty straight forward isnt it?
long date = Date.parse(request.getParameter("date")); //Wed Apr 03 00:00:00 BST 2013
String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
reportParams.put("p_date", formattedDate);
Caused by: java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:595)
Don't use Date.parse() to parse dates. As you can see in the API documentation, that method is deprecated, which means it is replaced by another method. The API documentation even mentions what you should use instead: DateFormat.parse().
Create a SimpleDateFormat object with the format that matches your input string, and use that to parse it into a Date object.
String text = "Wed Apr 03 00:00:00 BST 2013";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date date = df.parse(text);
The main issue is that you have the date at the end of the string. It should come after the month, e.g:
Wed, 03 Apr 2013 00:00:00 BST
Please read the documentation for a full description. Note also that Date.parse is deprecated in favour of DateFormat.parse.
1) Date.parse is deprecated
2) Date.parse API says It accepts many syntaxes; in particular, it recognizes the IETF standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also understands the continental U.S. time-zone abbreviations, but for general use, a time-zone offset should be used: "Sat, 12 Aug 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If no time zone is specified, the local time zone is assumed. GMT and UTC are considered equivalent. But your syntax is none of the described.
3) Use SimleDateFormat instead

Java : Convert string to Date

i try to convert string to date , but when after the conversion the month is set to jan , but in input the month is other example 'sep' . following is my code.
Date tempDate = new SimpleDateFormat("mm/dd/yyyy").parse("09/12/2014");
System.out.println("Current Date " +tempDate);
output :
Current Date Sun Jan 12 00:09:00 IST 2014
it is MM/dd/yyyy. not mm/dd/yyyy
Reference these formats Java Date Format Docs:
so mm is to reference minutes in hour while MM is for Month.
That is the reason you are getting:
Current Date Sun Jan 12 00:09:00 IST 2014 from ("09/12/2014")
by default month is Jan while it set minutes to '09' due to mm.

Java date parse

I'm attempting to parse the following string into a date object: 9/14/2012 9:50:56 PM
I'm using the following format:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss a");
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
But I keep getting the following date: Fri Sep 14 06:50:56 PDT 2012
I seem to be off by 12 hours (after accounting for the time change). However when I parse the following string: 9/14/2012 1:00:00 AM - I get the right date object: Thu Sep 13 22:00:00 PDT 2012
What I am doing wrong?
if your date is in am/pm format, you should use hh, instead of HH for hours. See the reference: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
What happens here is the 9 is treated as 09 hours in 24 hour format, which is 9 am, so your date is correctly pushed back 3 hours to make it 6 am. With the second date 1 am is 01 hours, and the date is correct.

Categories