首页 \ 问答 \ OffsetDateTime解析(OffsetDateTime parsing)

OffsetDateTime解析(OffsetDateTime parsing)

需要解析格式为2016-06-24T13:39:44.687680日期时间。

第一步使用,试图解析时间没有微秒的线。

System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME));

有例外

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more

试图在TimeZone使用DateTimeFormatter

System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault())));

类似的例外

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more

Need to parse Date time from format 2016-06-24T13:39:44.687680.

First step using, tried to parse time without microseconds with line.

System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME));

Having exception

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more

Tried to use DateTimeFormatter with TimeZone

System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault())));

The similar exception

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more

原文:https://stackoverflow.com/questions/38048032
更新时间:2023-10-25 22:10

最满意答案

您应该能够将<script>标记直接注入到具有所需数据的HTML中:

<script type="text/javascript">
var mynum = <?php echo intval($num); ?>;
var mystring = "<?php echo addslashes($string); ?>";
var myarray = <?php echo json_encode(array("one" => 1, "two" => 2)); ?>;
</script>

其他地方的任何JavaScript应该能够使用这些变量。

注意:如果使用这些数据的代码在页面的早期运行,他们显然不会看到这些变量。 如果是这样的话,要么确保首先加载数据,要么使用setTimeout()或document.onready事件来延迟代码的运行,直到页面完全加载。


You should be able to inject a <script> tag directly into the HTML with the data you want:

<script type="text/javascript">
var mynum = <?php echo intval($num); ?>;
var mystring = "<?php echo addslashes($string); ?>";
var myarray = <?php echo json_encode(array("one" => 1, "two" => 2)); ?>;
</script>

Any javascript elsewhere should be able to use these variables.

Note: if the code using this data runs earlier in the page, they obviously won't see these variables yet. If that's the case, either ensure that your data is loaded first, or you can delay the code from running until the page is fully loaded, using either setTimeout() or a document.onready event.

相关问答

更多
  • 正确使用jQuery,因为你已经在使用jQuery了。 $("#question").html(result.test_question); $('#aaa').val(result.test_question); Use jQuery properly since you're already using jQuery anyways. $("#question").html(result.test_question); $('#aaa').val(result.test_question);
  • 不幸的是,似乎没有什么能涵盖你正在寻找的东西。 我见过的最好的方法是使用gsp / html中的隐藏字段,并在客户端从javascript中检索这些值。 Unfortunately there does not seem to be anything that covers what you're looking for. The best approach that I have seen, and have used myself, is to use hidden fields in the gsp/ ...
  • 尝试以下方法: function GetQueryString(param) { var url = window.location.search.substring(1); var params = url.split("&"); for (i=0;i
  • 您应该能够将 其他地方的任何Java ...
  • 尝试这一个 y: 你已经在php标签中做了你想做的事 TRY THIS ONE y: you have do whatever your wish with in the php tag
  • 你可以做很多事情。 您必须知道的主要事情是javaScript在客户端 (浏览器) 上运行 ,而PHP在服务器上运行 。 然后,如果要将JS中的变量传递给PHP ,则必须进行服务器调用 。 您可以使用各种方式将变量从客户端发送到服务器。 据我从你的例子中了解,它看起来像你的PHP代码和你的javascript在同一个文件。 所以也许再打电话给你的文件就足够了。 假设您的文件名是index.php。