Unix time (AKA POSIX time or Epoch time) is defined as the number of seconds that have elapsed since January 1st 1970. Having time in a numerical format can be very useful. Often though, time data is recorded as timestamp strings.

A great way to convert a timestamp string into Unix time in Java is to use the SimpleDateFormat class (class documentation here). In the example below I create a SimpleDateFormat object by providing the timestamp “pattern” that I am expecting to see in the data. The parse() method will use this pattern to parse and convert the timestamp string into a date, and the getTime() method will convert that date into the number of milliseconds since January 1, 1970. Since we are only concerned about seconds and not milliseconds, we simply return the milliseconds divided by 1000.

This example can be seen the the Big Datums GitHub repo.

public static Integer tsToSec8601(String timestamp){
  if(timestamp == null) return null;
  try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date dt = sdf.parse(timestamp);
    long epoch = dt.getTime();
    return (int)(epoch/1000);
  } catch(ParseException e) {
     return null;

  public static void main(String[] args){

Leave a Reply

How to Convert Timestamp String to Unix Time (Epoch) in Java