1

EDIT: I have edited my question to include more information, I have tried many ways to do this already, asking a question on StackOverflow is usually my last resort. Any help is greatly appreciated.

I have a date (which is a Timestamp object) in a format of YYYYMMDDHHMMSS (e.g. 20140430193247). This is sent from my services to the front end which displays it in the format: date:'dd/MM/yyyy' using AngularJS.

How can I convert this into Epoch/Unix time?

I have tried the duplicate question that is linked to this, what I get returned is a different date.

I have also tried the following:

A:

//_time == 20140430193247
return _time.getTime()/1000; // returns 20140430193 - INCORRECT

B:

return _time.getTime(); // returns 20140430193247 (Frontend: 23/03/2608) - INCORRECT

C:

Date date = new Date();
//_time = 20140501143245 (i.e. 05/01/2014 14:32:45)
String str = _time.toString();
System.out.println("Time string is " + str);
//Prints: Time string is 2608-03-24 15:39:03.245 meaning _time.toString() cannot be used
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
        date = df.parse(str);
} catch (ParseException e) {
}

return date; // returns 20140501143245 - INCORRECT

D:

date = new java.sql.Date(_time.getTime());
return date; // returns 2608-03-24 - INCORRECT

The following shows the todays date correctly:

Date date = new Date();
return date; // returns 1398939384523 (Frontend: 01/05/2014)

Thanks

Cormac Q
  • 233
  • 1
  • 3
  • 10
  • 6
    Well what have you tried so far? (Hint: SimpleDateFormat for string parsing...) Also note that a "date in a particular format" isn't a `Timestamp` object. If you've got a `Timestamp`, can't you just call `getTime()` and divide by 1000? – Jon Skeet Apr 30 '14 at 17:04
  • Another hint: You need a timezone, too. – Meno Hochschild Apr 30 '14 at 17:05
  • 1
    A simple search for "java date format" would have given you the solution. Please at least attempt some research before posting questions. – indivisible Apr 30 '14 at 17:08
  • possible duplicate of [Convert DATE Format to Epoch](http://stackoverflow.com/questions/6687433/convert-date-format-to-epoch) – Basil Bourque May 01 '14 at 07:27
  • Hi, I have updated my question to include more information. I had already tried these ways, and I should have made that clearer. – Cormac Q May 01 '14 at 11:03
  • 1
    This is not a -6 question, come on people. – smeeb Nov 09 '16 at 14:15

2 Answers2

10

I got the answer after quite a while of trying different ways. The solution was pretty simple - to parse the time to a string as toString() didn't work.

    Date date;
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

    try {
        date = df.parse(String.valueOf(_time.getTime()));
    } catch (ParseException e) {
        throw new RuntimeException("Failed to parse date: ", e);
    }

    return date.getTime();
Cormac Q
  • 233
  • 1
  • 3
  • 10
  • These date-time classes are terrible. Now obsolete, having been supplanted by the modern java.time classes defined in JSR 310. – Basil Bourque Nov 08 '19 at 16:44
4

tl;dr

LocalDateTime
.parse(
    "20140430193247" , 
    DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" )
)
.atOffset(
    ZoneOffset.UTC
)
.toEpochSecond()

java.time

Parse your input string as a LocalDateTime as it lacks an indicator of offset-from-UTC or time zone.

String input = "20140430193247" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

Now we have a date with time-of-day, around half-past 7 PM on April 30 of 2014. But we lack the context of offset/zone. So we do not know if this was 7 PM in Tokyo Japan or 7 PM in Toledo Ohio US, two different moments that happened several hours apart.

To determine a moment, you must know the intended offset/zone.

If you know for certain that an offset of zero, or UTC itself, was intended, apply the constant ZoneOffset.UTC to get an OffsetDateTime.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

How can I convert this into Epoch/Unix time?

Do you mean a count of seconds or milliseconds since the first moment of 1970 in UTC?

For a count of whole seconds since 1970-01-01T00:00Z, interrogate the OffsetDateTime object.

long secondsSinceEpoch = odt.toEpochSecond() ;

For milliseconds, extract a Instant object. An Instant represents a moment in UTC, and is the basic building-block class of java.time. Then interrogate for the count.

Instant instant = odt.toInstant() ; 
long millisSinceEpoch = instant.toEpochMilli() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154