Problem with date format in Java

I have a problem with this code

public static void main(String[] args) throws IOException, ParseException { String strDate = "2011-01-12 07:50:00"; DateFormat formatter = new SimpleDateFormat("yyyy-mm-dd H:M:S"); Date date = formatter.parse(strDate); System.out.println("Date="+date); } 

Exit:
Date = Thu Feb 12 07: 01 : 00 EET 2015
What am I doing wrong?

+7
source share
4 answers

Uppercase M is the day of the month pointer. Lowercase m is the minute per hour. Also, you need lowercase s, since upper case S is milliseconds.

However, you need to be closer to this.

 yyyy-MM-dd HH:mm:ss 
+11
source

Your format is incorrect, see SimpleDateFormat .

Instead, you want

 String strDate = "2011-01-12 07:50:00"; DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
+3
source

This may be useful to use: Retrieve time from date String

Change "H: mm" to what you want the format to be.

+1
source

as your question that you check DateFormat formatter = new SimpleDateFormat ("yyyy-mm-dd H: M: S"); in this statement, "yyyy-MM-dd HH: mm: ss" is SimpleDateFormate.

0
source

All Articles