Java date parse - java

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.

Related

How to parse date with only month and year with SimpleDateFormat

I am working with expiration date of card. I have a API where I will get expiration date in "yyMM" format as "String". Here I am trying to use
SimpleDateFormat with TimeZone.getTimeZone("UTC")
So my code is like
String a= "2011";
SimpleDateFormat formatter = new SimpleDateFormat("yyMM");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(a);
System.out.println(date);
Now problem is, when I am passing 2011 the out it gives is Sat Oct 31 17:00:00 PDT 2020
Here you can see I am passing 11 as month but it is converting it to Oct instead of Nov.
Why?
And what other options I can use to convert string with yyMM to Date with Timezone?
You should use the Java 8 YearMonth class.
String a = "2011";
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyMM");
YearMonth yearMonth = YearMonth.parse(a, inputFormat);
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MMMM yyyy");
System.out.println(yearMonth.format(outputFormat));
Output
November 2020
You parsed it fine, but it's printed in PDT, your local timezone.
Sat Oct 31 17:00:00 PDT 2020
Well, Date doesn't track timezones. The Calendar class does, which is internal to the formatter. But still, default print behavior is current timezone.
If you logically convert this output back to UTC, and it will be November 1 since PDT is UTC-7.
Basically, use java.time classes. See additional information here How can I get the current date and time in UTC or GMT in Java?

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

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.

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.

Categories