Java – Get current Unix timestamp from date string

The following piece of Java code could convert a date string into a Unix timestamp.

String dateString = "Fri, 09 Nov 2012 23:40:18 GMT";
DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
Date date = dateFormat.parse(dateString);
long unixTime = (long)date.getTime()/1000;
System.out.println(unixTime); //<- prints 1352504418

 

Done =)

Reference:

http://stackoverflow.com/questions/13392030/unix-timestamp-creation-in-java

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.