I am trying to do a simple String to Date conversion in Java. I am skimming the date off some logs and need to convert it to a date to do some processing. A date coming through will look like this:
2012-09-07 19:53:33
In my code when I try to convert this into a Date object I get a completely different date. My code looks like this:
String taskStart = "2012-09-07 19:53:33";
String dateFormat = "yyyy-MM-dd hh:MM:ss";
SimpleDateFormat dateFormat=new SimpleDateFormat(format);
Date taskStartDate = dateFormat.parse(taskStart);
The output I get is this:
Sat May 07 19:00:27 PDT 2016
How can I just simply convert my original date to the correct format??
MM(month in year) twice. The m’s for “minute” must be lowercase.HHin order to capture hours that are specified in the range of0-23, as opposed tohhwhich expects AM/PM hours (hours in the range of1-12with an AM/PM specifier as part of the time string)formatvariable that you’re passing into the constructor for yourSimpleDateFormatobject. In fact, you’re using the variable namedateFormattwice and not defining theformatvariable at all – at least not according to the code that you included in the question.So, your proper format string, I believe, should be…
…and the full, proper code example would be: