java - Wrong hours converted to epoch time


Keywords:java 


Question: 

I'm trying to convert a date with epoch time method.
with these code below

long epoch = 0;
try {
    epoch = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("12/11/2017 23:20:23")
            .getTime();
} catch (ParseException e) {
    e.printStackTrace();
}

It gives me the epoch time: 1513052423

Which once convert give: Tuesday 12 December 2017 04:20:23 and not 23:20:23 :/


1 Answer: 

According to documentation SimpleDateFormat is locale-sensitive. Please check or set your timezone

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));