Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串时间戳解析为不支持的字段:InstantSeconds_Java_Parsing_Timestamp_Java 8 - Fatal编程技术网

Java 将字符串时间戳解析为不支持的字段:InstantSeconds

Java 将字符串时间戳解析为不支持的字段:InstantSeconds,java,parsing,timestamp,java-8,Java,Parsing,Timestamp,Java 8,我正在尝试将字符串转换为瞬间。你能帮我吗 我得到以下例外: 原因:java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:InstantSeconds at java.time.format.Parsed.getLong(Parsed.java:203) at java.time.Instant.from(Instant.java:373) 我的代码基本上是这样的 DateTimeFormatter formatter = DateT

我正在尝试将字符串转换为瞬间。你能帮我吗

我得到以下例外:

原因:java.time.temporal.UnsupportedTemporalTypeException:不支持的字段:InstantSeconds at java.time.format.Parsed.getLong(Parsed.java:203) at java.time.Instant.from(Instant.java:373)

我的代码基本上是这样的

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);

我使用的是Java 8 Update 72。

下面是如何获得具有默认时区的即时消息。您的字符串无法直接解析为Instant,因为缺少时区。因此,您始终可以获得默认值

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);

一个更简单的方法是在声明格式化程序对象时将默认时区添加到格式化程序对象中

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

首先使用日期格式将日期转换为util date,因为您的输入中没有时区。然后您可以将该日期转换为即时日期。这将为您提供准确的日期和时间

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));

我编写了这么简单的函数来执行转换:

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);
}
}
使用示例:

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

你的第一次输入不是瞬间的。缺少时区。我在尝试使用DateTimeFormatter.ofLocalizedDateTime 2-arg构造函数时遇到此错误,在internet上可以找到0个示例。它不喜欢FormatStyle.SHORT作为timeFormat第二个参数传递给该方法。顺便说一句,我最近要求在不事先知道确切格式的情况下解析到日期的字符串,实际上是尝试解析表示有效日期的任何字符串。所以我写了一篇关于我如何解决这个问题的文章。这里是链接:如果已经有明显更好的替代方案,不要提出本质上不安全的遗留API。我喜欢这个解决方案的紧凑性。我仍然会遇到这个错误,直到我注意到我无意中使用了
hh
,而不是
hh