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
Related
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");
I want to convert a string to date before storing it and I used
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date returnDate = format.parse(date);
When I ran this with sample date:
the input string for date conversion is 2014-05-06
the parsed date is Mon Jan 06 00:05:00 IST 2014
now when I store the returnDate in MySql the value is 2014-01-06 00:05:00
Why is the date changed ? Want to know if I am missing something. I went through the posts related to date string conversion : How to convert a date from a Datepicker to Mysql DATETIME format using java?
In your DateFormat use MM for month instead of mm, that is for minutes
Reference: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
You can use like this :
Date mDate= new Date(System.currentTimeMillis());
SimpleDateFormat mDateFormat= new SimpleDateFormat("dd MMM yyyy HH:mm a");
String dateformat=mDateFormat.format(mDate);
the string ["dd MMM yyyy HH:mm a"] can be changed according to need of formate.
Like in your case : "yyyy-mm-dd
I'm currently working on some simple project in Java and I have date in the following string:
String dateString = "Sun 7/14 03:44 AM 2013";
and want to to convert this string to Date object. I'm using following lines of code to do that. I searched site and found solution how to do this with DateFormatter:
DateFormat format = new SimpleDateFormat("EEE M/dd hh:mm a yyyy");
Date d = format.parse(dateString);
But I'm probably doing something wrong, because I always get exception:
Unparseable date: "Sun 7/14 03:44 AM 2013"
This seems to be problem with pattern I'm using but tried different patterns and nothing work.
Certain fields such as the day of week fields and/or AM/PM marker may not match those from your default Locale. ParseException has the method getErrorOffset to determine exactly where the pattern does not match.
try
DateFormat format =
new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
It is important to add Locale as you are parsing language day of week names.
String dateString = "Sun 7/14 03:44 AM 2013";
DateFormat format = new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.US);
Date d = format.parse(dateString);
I tried this out and the following worked,
String stringDate = "Sun 7/14 03:44 AM 2013";
DateFormat format = new SimpleDateFormat("EEE MM/dd hh:mm a yyyy");
System.out.println("Parsed Date = "+format.parse(stringDate));
The output was as follows
Parsed Date = Sun Jul 14 03:44:00 BST 2013
SimpleDateFormat formatter = new SimpleDateFormat("/* type your own format*/");
String formattedDate = formatter.format(todaysDate);
System.out.println("Formatted date is ==>"+formattedDate);
try this code
The modern answer for the sake of completeness. While the other answers were good answers in 2013, Date, DateFormat and SimpleDateFormat are now long outdated, and I recommend you replace them with their modern counterparts:
DateTimeFormatter parser
= DateTimeFormatter.ofPattern("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateString, parser);
The result is a LocalDateTime of 2013-07-14T03:44 as expected.
The format pattern string is still the same, and the need for an English language locale is the same.
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
I have this piece of simple code:
SimpleDateFormat sqlFormatter = new SimpleDateFormat ("YYYY-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
I get this output:
2012-03-09 12:00:00
Sun Jan 01 12:00:00 EST 2012
I know is supposed to be simple, but I am hoping someone can quickly see what I am missing.
I think your pattern is a little off. I'm suprised you're not seeing an IllegalArgumentException. Try using the following pattern with lower case y's and see if that resolves your issue:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Corrected code here:
SimpleDateFormat sqlFormatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println(temp);
Date last = sqlFormatter.parse(temp);
System.out.println(last);
You should have SimpleDateFormat instead of SimpleDateFormatter and for years you give yyyy instead of YYYY.
Once I corrected your format String - Y is not allowed, you need y - (and the typo already mentioned) it worked fine:
SimpleDateFormat sqlFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = "2012-03-09 12:00:00";
System.out.println (temp);
Date last = sqlFormatter.parse (temp);
System.out.println (last);
>2012-03-09 12:00:00
>Fri Mar 09 12:00:00 EST 2012
You need to use 'yyyy' and not 'YYYY'
Here is the output
2012-03-09 12:00:00
Fri Mar 09 12:00:00 IST 2012
for the pattern
yyyy-MM-dd HH:mm:ss