From SimpleDateFormat
:
Letter Date or Time Component <br />
y Year <br />
Y Week year
H Hour in day (0-23)
h Hour in am/pm (1-12)
So, use yyyy
for year and HH
for hours in the day. Also, you're separating the fields by -
, not by /
:
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(test);
After doing this, as @JigarJoshi suspects, you can format your Date
to look with another format:
String dateInDesiredFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH).format(date);
Or written as a complete block of code:
DateFormat parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
DateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss a", Locale.ENGLISH);
String test = "2014-04-01 05:00:00";
Date date = parse.parse(test);
System.out.println(format.format(date));
Produces the following output:
2014/04/01 05:00:00 AM