Pau
Pau

Reputation: 73

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

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

Upvotes: 7

Views: 5143

Answers (4)

Anil Reddaboina
Anil Reddaboina

Reputation: 601

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.

Upvotes: 0

Rich Adams
Rich Adams

Reputation: 26574

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

Upvotes: 3

rchanley
rchanley

Reputation: 459

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

Upvotes: 11

thotheolh
thotheolh

Reputation: 7450

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

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

Upvotes: 1

Related Questions