7

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);
}

The output is:
Date=Thu Feb 12 07:01:00 EET 2015
What am i doing wrong??

Pau
  • 73
  • 1
  • 5

4 Answers4

11

Upper case M is the day of the month designator. Lower case m is the minute in the hour. Also, you want lower case s, as upper case S is milliseconds.

That said, what you need may be closer to this

yyyy-MM-dd HH:mm:ss
rchanley
  • 459
  • 2
  • 7
3

Your format is wrong, see SimpleDateFormat.

You want this instead,

String strDate = "2011-01-12 07:50:00";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Rich Adams
  • 26,096
  • 4
  • 39
  • 62
1

This might have some use hopefully: Extract time from date String

Modify the 'H:mm' to whatever you want the format to be.

Community
  • 1
  • 1
thotheolh
  • 7,040
  • 7
  • 33
  • 49
0

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

Anil Reddaboina
  • 221
  • 1
  • 4