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??

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.