java - org.triten.bp.format.DateTimeParseException : Text '2018-07-22T14:00:00-03:00' could not be parsed at index 19

标签 java android datetime-parsing parseexception threetenbp

    public static String formatter(String dateInPattern, String dateOutPattern) {
    OffsetDateTime dateInPatternFormat = OffsetDateTime.parse(dateInPattern, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
    Date dateInValue = DateTimeUtils.toDate(Instant.parse(dateInPatternFormat.toString()));
    OffsetDateTime dateOutPatternFormat = OffsetDateTime.parse(dateOutPattern);

    return dateOutPatternFormat.format(DateTimeFormatter.ofPattern(dateInValue.toString()));

}

我需要以这种模式输入日期 yyyy-MM-dd'T'HH:mm:ss'Z' 这等于 (2018-07-22T14:00:00-03:00)。 我需要这种模式的输出 dd/MM/yyyy

请帮助我。

我在 Android 上遇到了很多日期问题:(

最佳答案

您的代码非常困惑,名称很奇怪,而且您似乎混淆了模式字符串,例如yyyy-MM-dd,带有字符串,例如2018-07-22

值字符串2018-07-22T14:00:00-03:00可以解析为OffsetDateTime,而无需指定DateTimeFormatter ,因为这是 OffsetDateTime 的默认格式。

如果您需要将其格式化dd/MM/yyyy,则使用DateTimeFormatter

不知道为什么你的方法需要 2 个参数。

示例:

String input = "2018-07-22T14:00:00-03:00";
OffsetDateTime offsetDateTime = OffsetDateTime.parse(input);
String output = offsetDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
System.out.println(output); // prints: 22/07/2018

关于java - org.triten.bp.format.DateTimeParseException : Text '2018-07-22T14:00:00-03:00' could not be parsed at index 19,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52545720/

相关文章:

java - ParseException 尝试从其中包含 + 的字符串中解析日期

java 到 mysql。我需要从字符串参数转换为时间戳

java - App Engine 应用程序性能测试

java - resttemplate.exchange 上的 SocketException 连接重置错误

java - 安卓/Java : How To Stop Download After 5 Seconds?

android - 使用camera2 API获取单张图片并用ImageView显示

C#如何复制excel的内容如你所见(例如: date time as you see is 12/31/2018, excel内容为43465之类的数字)

java - 带有参数的 Java 中的 XSL 转换

java - Selenium -Java : How to get absolute path to the file

android - setWebViewClient 与 setWebChromeClient 有什么区别?