Java SimpleDateFormat Unparseable date - java

I'm having this awkward problem with simple date format.
I'm parsing some strings from a file and want to convert them in Date object.
Strings are like
"2012-04-19 18:33:10"
so my code is:
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
sdf.setLenient(false);
Date d1 = sdf.parse("2012-04-19 18:33:10");
which gives me
java.text.ParseException: Unparseable date: "2012-04-19 18:33:10"
Without the
setLenient(false)
the output date is
Sun Jan 01 18:33:10 CET 2012
which is very incorrect.
I really don't understand why.
Any help would be appreciated.
Thanks in advance

Try:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Related

simpleDateformat not parsing when second is included

I am getting time in a particular format which I want to convert to another format. The below code snippet was taken from one of the stack overflow answers.
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);
This code format is working as long as my input date is of the following format: 22/08/2015 14:23.
But when i try with seconds, i get the error:
Unparseable date: "06/03/2019 14:17:25"
SimpleDateFormat origDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date retrivedDate = origDateFormat.parse(origFormat);
return newDateFormat.format(retrivedDate);
Here's the javadoc of parse() method and this is what it says:
Parses text from the beginning of the given string to produce a date.
The method may not use the entire text of the given string.
So, as per the documentation, you can parse 06/03/2019 14:17:25 dates with both dd/MM/yyyy HH:mm and dd/MM/yyyy HH:mm:ss date formats. If you parse with the format that doesn't contain seconds then it will ignore the second part. Below is the working code:
String date = "06/03/2019 14:17:25";
SimpleDateFormat mmDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
SimpleDateFormat ssDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(mmDateFormat.parse(date));
System.out.println(ssDateFormat.parse(date));
Below are the outputs:
Wed Mar 06 14:17:00 GMT 2019
Wed Mar 06 14:17:25 GMT 2019

SimpleDateFormat parsing is not correct

I'm using SimpleDateFormat to parse Date String ase bellow:
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
System.out.println(formatter.parse(firstDate));
And for String "2011-04-02" i got after parsing "Sun Jan 02 00:00:00 EET 2011" so Month is Jan there but must be Apr.
What i'm doing wrong?
DD is day in year. You need dd (lowercase) instead, which is day in month.
See documentation of SimpleDateFormat for more info.
Try: SimpleDateFormat("yyyy-MM-dd").
DD is day of year, that's why you got 2nd of January.
try this
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.parse(firstDate));
Your date format incorrect. use "yyyy-MM-dd"
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(formatter.parse("2011-04-02"));
Live Demo

Java new Date with time stamp in date format

I have used the following
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss zzz");
Date date = new Date();
String formattedDate= df.format(date);
Date dateWithTime = df.parse(formattedDate);
i got the formatted date as string when i conver this into date i got error like
java.text.ParseException: Unparseable date: "Tue Feb 26 11:45:43 IST 2013"
How would convert to date or how i format a current date and get as date?
I think your code wouldn't throw ParseException. But it sure would definitely yield wrong output. your format should be:
"dd-MM-yyyy hh:mm:ss zzz"
Note that
MM---> Months
mm---> Minutes
Test with your code with out correcting the format:
Sat Jan 26 06:24:07 GMT 2013
Test with your code with correcting the format:
Tue Feb 26 06:20:51 GMT 2013
The date format should be as follows as shown in the exception. Change it to -
EEE MMM d hh:mm:ss z yyyy
The correct simpledateformat will be
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Please refer the link for proper date formatting and parsing
SimpleDateFormat

Why is "2012-30-03" parsed as June 3 2014 with DateFormat?

I have a string (ex: 2012-30-03 12:30), and I am trying to use the following code:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
this.startTime = (Date) df.parse(startTime); // startTime = "2012-30-03 12:30"
But when I try the following:
System.out.println(this.startTime);
The following get printed
Tue Jun 03 12:30:00 CEST 2014
What the heck is wrong here?
Is there a better way to turn the kind of string I have into a working date object?
I think
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
should be
DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm");
based on the example string you provided (2012-30-03 12:30).
Focus your rage against the date class into verifying the basics and you'll be ok. ;)

How do I parse a standard date into GMT?

Can someone show me a piece of java code that parses this date:
2009-08-05
INTO THIS GMT DATE:
2009/217:00:00
====
what i have so far is:
java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
format.setCalendar(cal);
java.util.Date date = format.parse(sdate);
but its not working
Here is the format you're looking for:
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2009-08-05");
String parsedDate = new SimpleDateFormat("yyyy/D:HH:mm").format(date);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
That's how to set it to GMT at least. Not sure where you are getting 2009/217 from 2009-08-05
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormatGmt.format(new Date())+"");
This will convert your local time to GMT.

Categories

Resources