java.text.ParseException:无法解析的日期:“20:01:00.000Z" [英] java.text.ParseException: Unparseable date: "20:01:00.000Z"

查看:40
本文介绍了java.text.ParseException:无法解析的日期:“20:01:00.000Z"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以10:30:00.000Z"格式获取时间字段的值.我想将其转换为 10:30 AM/PM 格式.我正在尝试使用 SimpleDateFormat 并解析它,但我收到了 java.text.ParseException.有人可以帮我吗?

I am getting a value for my time field in this format "10:30:00.000Z". I want to convert it to 10:30 AM/PM format. I'm trying to make use of SimpleDateFormat and parse it but I'm getting java.text.ParseException. Can someone help me on this?

treatmentObject.getTime() 的值 = "10:30:00.000Z"

Value of treatmentObject.getTime() = "10:30:00.000Z"

'''

if (treatmentObject.getTIME1() != null) {  
System.out.println("Treatment time is"+treatmentObject.getTIME1());  
time1 = toISO8601UTC(fromISO8601UTC(treatmentObject.getTIME1()));
System.out.println("Time1  is" + time1);  
}  

public static String toISO8601UTC(Date date) {
System.out.println("date is::" + date);
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("HH:mm:ss.SSSZZZZZ");
df.setTimeZone(tz);
System.out.println("String date is::" + df.format(date));
return df.format(date);
}  

public static Date fromISO8601UTC(String dateStr) {
System.out.println("dateStr is::" + dateStr);
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("hh:mm a");
df.setTimeZone(tz);
    try {
    System.out.println("parse dateStr is::" + df.parse(dateStr));
    return df.parse(dateStr);
    } catch (ParseException e) {
        System.out.println("Inside catch exception");
        e.printStackTrace();
    }
    return null;
}

'''

推荐答案

java.time

    String treatmentTimeString = "20:01:00.000Z";
    DateTimeFormatter timeFormatter
            = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
                    .withLocale(Locale.US);
    OffsetTime treatmentTime = OffsetTime.parse(treatmentTimeString);
    OffsetTime timeInUtc = treatmentTime.withOffsetSameInstant(ZoneOffset.UTC);
    String formattedTime = timeInUtc.format(timeFormatter);
    System.out.println("Treatment time: " + formattedTime);

这个片段的输出是:

治疗时间:晚上 8:01

Treatment time: 8:01 PM

我正在使用 java.time,现代 Java 日期和时间 API.我进一步利用了这样一个事实,即您的时间字符串符合 ISO 8601,java.time 类将标准格式解析(并打印)作为它们的默认格式,即没有任何显式格式化程序.对于格式化,我使用 Java 的内置本地化格式.这样我就不用自己编写任何格式模式字符串,否则这总是容易出错的任务.

I am using java.time, the modern Java date and time API. I am further exploiting the fact that your time string conforms with ISO 8601, the standard format the the java.time classes parse (and also print) as their default, that is, without any explicit formatter. For formatting I am using Java’s built-in localized format. In this way I am free from writing any format pattern string myself, which is otherwise always an error-prone task.

我正在将解析的时间转换为 UTC.在示例中,这不是必需的,因为示例字符串中的 Z 表示它已经在 UTC 中.如果有一天你得到一个具有不同偏移量的时间字符串,无论如何都要这样做,确保你也能得到正确的 UTC 时间.

I am converting the parsed time to UTC. In the example this is not necessary because the Z in the example string indicates that it is already in UTC. Doing it regardless makes sure that you will also get the correct UTC time if one day you get a time string with a different offset.

它给出了预期的输出,但问题是,它适用于最低 API 级别 26.我的最低 API 级别 21.那么有什么解决方法吗?

It is giving the expected output but the problem is, it works with minimum API Level 26. My minimum api level 21. So any workaround?

是的,当然,java.time 在新旧 Android 设备上运行良好.它只需要至少 Java 6.

Yes, certainly, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 开始)中,内置了现代 API.
  • 在 Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接).
  • 在(旧版)Android 上使用 ThreeTen Backport 的 Android 版本.它被称为 ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

看来你交换了你拥有的格式和你想要的格式.您有 20:01:00.000Z 并尝试使用格式模式为 hh:mm a 的格式化程序来解析它.由于您报告的异常,这显然失败了.之后,您尝试使用格式模式 HH:mm:ss.SSSZZZZZ 格式化您希望通过格式化程序解析获得的 Date.如果你已经走到这一步,你会看到它给了你 20:01:00.000+0000,所以不是 10:30 AM/PM 格式.

It seems you swapped the format you had and the format you wanted. You had 20:01:00.000Z and tried to parse it with a formatter with format pattern hh:mm a. This obviously failed with the exception you reported. After that you tried to format the Date you had expected to get from parsing with a formatter with format pattern HH:mm:ss.SSSZZZZZ. If you had got this far, you would have seen that it gave you 20:01:00.000+0000, so not 10:30 AM/PM format.

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
  • Wikipedia article: ISO 8601

这篇关于java.text.ParseException:无法解析的日期:“20:01:00.000Z"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆