SimpleDateFormat producing incorrect time on parsing "YYYY-MM-dd HH: mm"

I am trying to parse String ( YYYY-MM-dd HH:mm ) to Date , but getting the wrong date than expected.

CODE:

 Date newDate = null; String dateTime = "2013-03-18 08:30"; SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd HH:mm", Locale.ENGLISH); df.setLenient(false); try { newDate = df.parse(dateTime); } catch (ParseException e) { throw new InvalidInputException("Invalid date input."); } 

It produces:

Sun Dec 30 08:30:00 EST 2012 (false)

I tried disabling Lenient but no luck.

Update

Thanks Sudhanshu for the answer, this helped me solve the Java conversion. When I enter the return date from the above code into the database, I correctly give the date, but the time is always 00:00 .

 ps.setDate(3, new java.sql.Date(app.getDate().getTime())); 
+6
source share
4 answers

Yyyy must be yyyy -

 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH); 

Please check the documentation for SimpleDateFormat here
Java 6: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Java 7: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

+32
source

Use small case Y, not caps. those. yyyy not yyyy

Check out the comments here: Simple Java date format and other referenced answers.

+6
source

There are two problems.

  • The format string should be "yyyy-MM-dd HH:mm" .
  • The data type for storing TimeStamp time, not Date in the database.

Correct both things and you will be able to store and retrieve the date over time.

+4
source

Here is a modern answer. The other answers were great answers when they were written in 2013. A year later, a modern API with a date and time appeared as a replacement for the old Date , SimpleDateFormat and friends classes. IMHO you should use new classes now. They are much more friendly to programmers.

  LocalDateTime newDate = null; String dateTime = "2013-03-18 08:30"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm", Locale.ENGLISH); try { newDate = LocalDateTime.parse(dateTime, dtf); } catch (DateTimeParseException e) { throw new InvalidInputException("Invalid date input."); } 

With the exception of class names, this is not what differs from the old code. There will be differences in other examples, usually modern classes provide clearer code and fewer opportunities for errors.

For just a small demonstration of the difference, try using the format template template that you had with the uppercase YYYY in it:

  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm", Locale.ENGLISH); 

Now the code throws a java.time.format.DateTimeParseException: Text '2013-03-18 08:30' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=3, DayOfMonth=18, WeekBasedYear[WeekFields[SUNDAY,1]]=2013},ISO resolved to 08:30 of type java.time.format.Parsed . Its long, I know. It should be noted that among the fields that he mentions, there is no year, but only WeekBasedYear. It is not the same thing; and you may already have realized that this is precisely because the uppercase Y is for the weekly year (only for the user with the week number), where you must use lowercase Y for the year. I find this behavior a little more useful than what the old classes did: they gave you a result that was wrong, pretended to be all right and left you completely in the dark about what was wrong.

Further, I understand that you want to save the date-date in the database. In the old days, you convert to a suitable java.sql type. Do not need anymore. I believe that the JDBC 4.2 driver can simply be done:

  ps.setObject(3, newDate); 

I have not tested this with a database. Note that instead of setDate() you are using setObject() .

+1
source

All Articles