Java : Convert string to Date - java

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.

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?

Formatting a date in java

I have the two Date objects which I am trying to format from being in MM/DD/YYYY format to "yyyy-mm-dd HH:mm:ss" format.
The current approach I am using is to first format those dates using SimpleDateFormat which will return two Strings, then I have to convert this string back to Date to get the formatted final Date objects.
So I was wondering if there was a simpler way to change the Date object format without going in many steps?
Thanks
The format is irrelevant. Date simply represents the number of milliseconds since the Unix epoch.
Remember, Date has no concept of format, it doesn't care.
You should simply format the Date object with whatever formatters you need...
For example...
Date date = new Date();
System.out.println(date);
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(date));
System.out.println(new SimpleDateFormat("yyyy MMMM EE").format(date));
System.out.println(new SimpleDateFormat("EEEE MMMM yyyy").format(date));
System.out.println(date);
Outputs...
Wed Jan 22 11:55:18 EST 2014
22/01/2014 11:55:18 AM
22/01/2014
2014 January Wed
Wednesday January 2014
Wed Jan 22 11:55:18 EST 2014
Note how the first and last values don't change. Date has no internal concept of format, that's the responsibility of the formatter.
For example, if I took the String value 22/01/2014 and parsed it back to a Date using SimpleDateFormat
Date date = new SimpleDateFormat("dd/MM/yyyy").parse("22/01/2014");
And then outputted the date value...
System.out.println(date);
It would output something like...
Wed Jan 22 00:00:00 EST 2014
The format has being lost. It would need to use an appropriate formatter to change what is displayed

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 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.

Convert from Unix Epoch to day and time in Java

I need to convert a UNIX epoch time to day and time something like:
e.g. 1309778593 to Monday, 11:23:12
Any help?
You can do this with help of following code-
String epochTime = "1309778593";
Date convertedDate = new Date(Long.parseLong(epochTime) * 1000);
System.out.println(convertedDate);
This will print-
Mon Jul 04 13:23:13 CEST 2011
I think this will help!
Pull the date components from this:
new Date(1309778593)

Categories