- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试将字符串从 JSON 转换为 ZonedDateTime,就像
static String getWatchTime(JSONObject aJson, JSONObject bJson) {
long difference = 0 ;
try {
String aTime = aJson.getString("time_utc_8");
String bTime = bJson.getString("time_utc_8");
String pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS";
DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern).ISO_DATE;
System.out.println(aTime);
ZonedDateTime a = ZonedDateTime.parse(aTime, Parser);
ZonedDateTime b = ZonedDateTime.parse(bTime, Parser);
ChronoUnit unit = null;
difference = unit.between(a, b);
System.out.println(difference);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String t = difference +"";
return t;
}
总是报错
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-06-28 22:29:44.700228' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2016-06-28T22:29:44.700228 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.OffsetDateTime.parse(Unknown Source)
at Q2.getWatchTime(Q2.java:321)
at Q2.WatchTime(Q2.java:265)
at Q2.main(Q2.java:31)
我想得到这两个日期之间的差异。我试过 SimpleDateFormat
但它会得到错误结果,对于工厂来说。
最佳答案
我觉得评论里都已经说完了,所以这里只是总结一下。
(1) 您的格式模式字符串是正确的。您只需要从以下行中删除.ISO_DATE
,这样它就变成了:
DateTimeFormatter Parser = DateTimeFormatter.ofPattern(pattern);
(ISO_DATE
接受例如“2011-12-03+01:00”或“2011-12-03”,一个没有时间的日期,有或没有与 UTC 的偏移;你有据我所知,这里没有任何用处。)
(2) 由于您的字符串似乎既没有时区也没有偏移量,因此使用 LocalDateTime
:
LocalDateTime a = LocalDateTime.parse(aTime, Parser);
LocalDateTime b = LocalDateTime.parse(bTime, Parser);
如果计算差值时需要考虑夏令时(DST)等,解析后转换时间:
ZoneId timeZone = ZoneId.systemDefault();
ZonedDateTime a = LocalDateTime.parse(aTime, Parser).atZone(timeZone);
ZonedDateTime b = LocalDateTime.parse(bTime, Parser).atZone(timeZone);
请仔细考虑用于转换的时区,以确保您获得预期的结果。
(3) null
的 ChronoUnit
将不起作用。 我不知道你想要哪个,所以这个选项是随机挑选:
ChronoUnit unit = ChronoUnit.DAYS;
通过这三项更改,您的方法在我的计算机上执行得很好。在一次运行中它打印出:
2016-06-28 22:29:44.700228
365
在同一次运行中,它返回了一个字符串 365
。
关于java - 无法将字符串转换为 ZonedDateTime : DateTimeParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44487065/
从 https://api.spacexdata.com/v3/launches 获取日期此日期的格式为:2006-03-25T10:30:00+12:00。我想将它转换为“dd, mm, yyyy”
从 https://api.spacexdata.com/v3/launches 获取日期此日期的格式为:2006-03-25T10:30:00+12:00。我想将它转换为“dd, mm, yyyy”
我正在尝试使用 ThreeTenBP 将日期字符串从服务器转换为日期时间。我的方法如下所示: String toDateTime(String dateString) { DateTimeFo
尝试更改字符串的日期格式但出现 DateTimeException: String oldDate = "2018-12-18T17:04:56+00:00"; String outputFormat
我正在尝试使用 Java OffsetDateTime 解析日期字符串,但出现异常。 字符串如下所示:"20101217180000GMT+0800" 我的方法是这样的: OffsetDateTime
这个问题在这里已经有了答案: Exception when trying to parse a LocalDateTime (1 个回答) 关闭 4 年前。 基本上,我试图将字符串解析为时间戳。 p
我正在从 SQLite 数据库加载一个字符串,然后尝试将其转换为 LocalDate,以便我可以将其添加为对象参数,然后比较列表中对象的日期。 但是应用程序崩溃并给出 DateTimeParseExc
我正在将正常日期解析为 LocalDate 格式。在转换时,我收到一个异常调用。 Caused by: java.time.format.DateTimeParseException: Text '2
以下代码抛出 DateTimeParseException: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd
我正在尝试从 sql 中解析时间戳对象并且遇到了问题,这是我所拥有的: DateTimeFormatter dateDTF = DateTimeFormatter.ofLocalizedDateTi
我尝试将字符串从 JSON 转换为 ZonedDateTime,就像 static String getWatchTime(JSONObject aJson, JSONObject bJson) {
使用的jdk:1.8 不确定问题是什么,configuredFormat 是有效的,inputTime 也是有效的,真的很困惑问题是什么。 public class Test { public
我正在尝试解析以下日期“Wed, 26 Feb 2020 03:42:25 -0800” String datePattern = "EEE, dd MMM yyyy HH:mm:ss Z"; Dat
我需要以年、月、日和秒为单位计算年龄,我使用 jcalendar 来获取用户输入,但我不断收到 DateTimeParseException 错误,有人可以告诉我如何解决这个问题吗? JDat
这个问题在这里已经有了答案: Is java.time failing to parse fraction-of-second? (3 个答案) 关闭 7 年前。 LocalDateTime.par
目标 为可以处理以下格式之一的输入的 LocalDate 实例提供灵活的解析器: 年 年月 年月日 实现尝试 下面的类尝试处理第一个和第二个模式。解析适用于年份输入,但年份 + 月份会导致下面列出的异
我想用 DateTimeFormatter 将像 Wed, 21 Oct 2016 07:28:00 GMT 这样的 GMT 日期解析成 Instant .为了创建模式,我使用了官方文档:DateTi
我正在尝试将字符串转换为 Java 11 中的 Date 对象。下面的代码给出了异常。尝试了不同的方法,到目前为止没有运气。对解决此错误消息有任何帮助吗? String date = 'Mon Aug
我正在尝试解析日期以将其转换为纪元。我尝试了 similar question here 的解决方案没有成功: String date = "Jun 4 2015"; DateTimeForma
我正在尝试使用 Java 8 DateTimeFormatter 类来解析西类牙语的时间字符串。经过几个小时的搜索,我找不到任何提示。返回的错误是: java.time.format.DateTime
我是一名优秀的程序员,十分优秀!