user3576299
user3576299

Reputation: 15

How to safely convert time to unix timestamp in Java?

It seems the following doesn't work properly. For example for value "2014-04-10 12:58:23.0" and "2014-04-10 00:58:23.0", it yields same answer.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Long timestamp = dateFormat.parse(date).getTime()/1000L;

Upvotes: 0

Views: 35

Answers (1)

Wander Nauta
Wander Nauta

Reputation: 19615

It looks like your input strings are in 24-hour time format while your format string specifies 12-hour time. Try using H instead of h.

Source: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Upvotes: 2

Related Questions