1

I have a database filled with timestamps in the following format:

Wed Feb 29 20:56:47 +0000 2012
Wed Feb 29 00:32:48 +0000 2012
Fri Apr 01 00:10:30 +0000 2011

I need to turn the into Epoch times to easily do calculations.

I want them to be seconds since 1970 (like what System.currentTimeMillis()/1000 does)

Does java have some method for this or do I have to parse and calculate it? Thanks in advance.

Patrick Lorio
  • 5,520
  • 11
  • 45
  • 74

2 Answers2

3

Well you have to parse it, but that's easy enough to do with SimpleDateFormat:

SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy",
                                               Locale.US);

Date date = format.parse(text);
long secondsSinceEpoch = date.getTime() / 1000;

On the other hand, it would be better if you stored the values in your database in a more appropriate column type to start with. Are you really storing them as text? If so, why?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I just need a standard. Epoch seems like the easiest to use for me. I need to calculate time intervals and convert to different formats in different code languages. – Patrick Lorio Mar 03 '12 at 17:23
  • @PatrickLorio: But why use *text* in the database at all? Surely your database has some sort of DATETIME field type, and even if it doesn't it would be much more efficient to just store the number of milliseconds or seconds since the epoch *as a number*. Why go through parsing and formatting all the time? (Note that with a non-text format, you'd be able to calculate time intervals *in SQL*, as well as performing comparisons for filtering etc.) – Jon Skeet Mar 03 '12 at 17:25
  • Sorry I don't think I properly answered you question. I will be storing the epoch times as numbers. I will only be formatting it once into the database. And for converting it into different formats will be on the client side. Also I won't be doing SQL operations, it's more just a place to store my data not crunch through it. – Patrick Lorio Mar 03 '12 at 18:09
  • 1
    @PatrickLorio: But why bother storing it in text in the database at all? Why introduce conversions for no reason? Just keep it as "seconds the epoch" or a more strongly-typed value everywhere. – Jon Skeet Mar 03 '12 at 18:12
2

Solution using java.time, the modern API:

Your date-time string has timezone offset value and therefore, it's a candidate to be parsed into OffsetDateTime object which you can convert to Instant. The function, Instant#toEpochMilli converts an instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d H:m:s X u", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse("Wed Feb 29 20:56:47 +0000 2012", dtf);
        Instant instant = odt.toInstant();
        long epochSecond = TimeUnit.SECONDS.convert(instant.toEpochMilli(), TimeUnit.MILLISECONDS);
        System.out.println(epochSecond);
    }
}

Output:

1330549007

Try to avoid performing calculations yourself if you have standard API available (e.g. TimeUnit#convert used in the code above) to achieve what you would do with the calculation.

Learn more about java.time, the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110