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??
Try with SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy", Locale.US);
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.
Valid values MM, MMM or MMMMM. To display day of month. Valid values d, dd. To display hour of day (1-12 AM/PM).
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. }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With