Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Date Format in Java

Tags:

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

like image 370
Pau Avatar asked Jan 12 '11 06:01

Pau


People also ask

How parse DD MMM YYYY in Java?

Try with SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy", Locale.US);

How do I create a date in YYYY MM DD format in Java?

Creating A Simple Date Format A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.

What are the valid date formats in Java?

Valid values MM, MMM or MMMMM. To display day of month. Valid values d, dd. To display hour of day (1-12 AM/PM).

How do you check if the date is in dd mm yyyy format in Java?

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); formatter. setLenient(false); try { Date date= formatter. parse("02/03/2010"); } catch (ParseException e) { //If input date is in different format or invalid. }


1 Answers

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
like image 163
rchanley Avatar answered Oct 14 '22 14:10

rchanley