0
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class HelloWorld{

 public static void main(String []args){
    String s = "Sep 01, 2014 6:30 pm";
    SimpleDateFormat timeFormat = new SimpleDateFormat("MMM dd, YYYY hh:mm a", Locale.US);
    try{
    Date startTime = timeFormat.parse(s);
    System.out.println(startTime);
    }catch(Exception e){}
 }
}

The result given is Sun Dec 29 18:30:00 CST 2013, which is totally wrong

Please help!!

user2534365
  • 215
  • 1
  • 2
  • 10
  • possible duplicate of [SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"](http://stackoverflow.com/questions/15916958/simpledateformat-producing-wrong-date-time-when-parsing-yyyy-mm-dd-hhmm) – SparkOn Sep 06 '14 at 14:08

2 Answers2

4

change

SimpleDateFormat timeFormat = new SimpleDateFormat("MMM dd, YYYY hh:mm a", Locale.US); to

SimpleDateFormat timeFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a", Locale.US);

As Y stands for the Week year refer

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

More explanation on week year you can find here

SparkOn
  • 8,806
  • 4
  • 29
  • 34
2

Try with little y for year instead of big Y.

See link

khelwood
  • 55,782
  • 14
  • 81
  • 108