keiki
keiki

Reputation: 3459

Parse String timestamp to Instant throws Unsupported field: InstantSeconds

I am trying to convert a String into an Instant. Can you help me out?

I get following exception:

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds at java.time.format.Parsed.getLong(Parsed.java:203) at java.time.Instant.from(Instant.java:373)

My code looks basically like this

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = "2016-02-16 11:00:02";
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
Instant result = Instant.from(temporalAccessor);

I am using Java 8 Update 72.

Upvotes: 57

Views: 76501

Answers (4)

Waldek
Waldek

Reputation: 1

I wrote such simple function to perform the conversion:

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.time.Instant;
import java.sql.Timestamp;


public class SqlTimestampParser {

public static Timestamp parseTimestamp(String dateTime, String format) throws Exception {
    if (format == null || format.trim().length() == 0) {
        throw new Exception("No format defined!");
    }
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    ZonedDateTime timed = ZonedDateTime.parse(dateTime, formatter);
    timed.format(formatter);
    Instant x = Instant.from(timed);
    return Timestamp.from(x);
}
}

With sample usage:

SqlTimestampParser.parseTimestamp('2020-09-17 16:20:35.294000+00:00',"yyyy-MM-dd HH:mm:ss.SSSSSSXXX") 

Upvotes: 0

Michael Gantman
Michael Gantman

Reputation: 7790

Here is how to get an Instant with a default time zone. Your String can not be parsed straight to Instant because timezone is missing. So you can always get the default one

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

String timestamp = "2016-02-16 11:00:02";
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.systemDefault());
Instant result = Instant.from(zonedDateTime);

Upvotes: 51

Parthiban
Parthiban

Reputation: 557

First convert your date into util date using date format as you don't have time zone in your input. Then you can convert that date into Instant date. This will give you date with accurate time.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = "2016-02-16 11:00:02";
Date xmlDate = dateFormat.parse(timestamp);
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Instant instantXmlDate = Instant.parse(dateFormat.format(xmlDate));

Upvotes: -4

jdex
jdex

Reputation: 1363

A simpler method is to add the default timezone to the formatter object when declaring it

final DateTimeFormatter formatter = DateTimeFormatter
                                    .ofPattern("yyyy-MM-dd HH:mm:ss")
                                    .withZone(ZoneId.systemDefault());
Instant result = Instant.from(formatter.parse(timestamp));

Upvotes: 63

Related Questions