简体   繁体   English

Kotlin DateTimeParseException

[英]Kotlin DateTimeParseException

Getting date from https://api.spacexdata.com/v3/launches This date have format: 2006-03-25T10:30:00+12:00.https://api.spacexdata.com/v3/launches获取日期 此日期的格式为:2006-03-25T10:30:00+12:00。 I want convert it to "dd, mm, yyyy", but always getting error: "java.time.format.DateTimeParseException: Text '2006-03-25T10:30:00+12:00' could not be parsed, unparsed text found at index 10"我想将其转换为“dd,mm,yyyy”,但总是出错:“java.time.format.DateTimeParseException: Text '2006-03-25T10:30:00+12:00' could not be parsed, unparsed text在索引 10 处找到"

my code:我的代码:

val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)

var launchDateConverted: String=  myDate.toString()

i getting data at String, then i convert it to date for formatting, and after i converting date back to string thats to display at UI.我在字符串中获取数据,然后将其转换为日期以进行格式化,然后将日期转换回字符串以显示在 UI 上。 i used different methods, but cannot find the correct way.我使用了不同的方法,但找不到正确的方法。 My current locale is "RU".我当前的语言环境是“RU”。

You do not need any formatter to parse your input string您不需要任何格式化程序来解析您的输入字符串

You input string, 2006-03-25T10:30:00+12:00 is already in the default format used by OffsetDateTime#parse and therefore, you do not need to use a formatter explicitly in order to parse your input date-time string.您输入字符串, 2006-03-25T10:30:00+12:00已经是OffsetDateTime#parse使用的默认格式,因此,您不需要显式使用格式化程序来解析输入的日期时间字符串.

m specifies the minute while M specifies the month m指定分钟,而M指定月份

You have wrongly used m for the month for which the symbol is M .您错误地将m用于符号为M的月份。 Check the documentation page to learn more about these symbols.查看文档页面以了解有关这些符号的更多信息。

Prefer u to y喜欢u y

y specifies the year-of-era (era is specified as AD or BC ) and is always a positive number whereas u specifies the year which is a signed (+/-) number. y指定时代的年份(时代指定为ADBC )并且始终为正数,而u指定年份,它是带符号的 (+/-) 数字。 Normally, we do not use + sign to write a positive number but we always specify a negative number with a - sign.通常,我们不使用+号来写入正数,但我们总是用-号指定负数。 The same rule applies for a year .同样的规则适用于一年 As long as you are going to use a year of the era, AD (which is mostly the case), both, y and u will give you the same number.只要您要使用时代的年份, AD (大多数情况下), yu都会给您相同的数字。 However, the difference occurs when you use a year of the era, BC eg the year-of-era , 1 BC is specified as year , 0 ;但是,当您使用时代的年份时会出现差异,例如BC年份, 1 BC指定为year , 0 the year-of-era , 2 BC is specified as year , -1 and so on. year-of-era , 2 BC被指定为year , -1等等。 You can understand it better with the following demo:您可以通过以下演示更好地理解它:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Testing {
    public static void main(String[] args) {
        System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
        System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
        System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));

        System.out.println();

        System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
        System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
        System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));

        System.out.println();

        System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
        System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
        System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
    }
}

Output: Output:

-1 1 1
2 1 1
2BC 1 1

0 1 1
1 1 1
1BC 1 1

1 1 1
1 1 1
1AD 1 1

Note: I've used Java to demonstrate the solution but it will also work in Kotlin.注意:我使用 Java 来演示解决方案,但它也适用于 Kotlin。

The final solution:最终的解决方案:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2006-03-25T10:30:00+12:00");
        System.out.println(odt);

        // Format it into the desired pattern
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd, MM, uuuu", Locale.US);
        String formatted = dtf.format(odt);
        System.out.println(formatted);
    }
}

Output: Output:

2006-03-25T10:30+12:00
25, 03, 2006

Learn more about the modern date-time API from Trail: Date Time .Trail: Date Time了解有关现代日期时间 API 的更多信息。

  1. Your formatter does not match the input format.您的格式化程序与输入格式不匹配。 Basically you need two formatters: one for input and one for output.基本上你需要两个格式化程序:一个用于输入,一个用于 output。
  2. The format "dd, mm, yyyy" is wrong: mm stands for minute of hour, not for month.格式“dd, mm, yyyy”是错误的:mm 代表小时的分钟,而不是月。 You should use "dd, MM, yyyy".您应该使用“dd, MM, yyyy”。
val launchDate = "2006-03-25T10:30:00+12:00"

val inputFormatter = DateTimeFormatter.ISO_DATE_TIME
val myDate = LocalDate.parse(launchDate, inputFormatter)

val outputFormatter = DateTimeFormatter.ofPattern("dd, MM, yyyy", Locale.US)
println(outputFormatter.format(myDate))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ThreeTenABP:DateTimeParseException - ThreeTenABP: DateTimeParseException DateTimeParseException通过解析日期字符串 - DateTimeParseException by parsing a date string DateTimeParseException:文本无法解析为持续时间 - DateTimeParseException: Text cannot be parsed to a Duration 将字符串解析为LocalDateTime时出现DateTimeParseException - DateTimeParseException when parsing String to LocalDateTime DateTimeParseException - 无法在索引 15 处解析 - DateTimeParseException - could not be parsed at index 15 尝试执行ZonedDateTime.parse时出现DateTimeParseException - DateTimeParseException while trying to perform ZonedDateTime.parse 使用DateTimeFormatter的java.time.format.DateTimeParseException - java.time.format.DateTimeParseException using DateTimeFormatter DateTimeParseException:无法解析文本:无法从TemporalAccessor获取LocalDateTime - DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor java.time.format.DateTimeParseException: 无法在索引 0 处解析文本“2020052” - java.time.format.DateTimeParseException: Text '2020052' could not be parsed at index 0 java.time.format.DateTimeParseException:无法在索引 0 处解析 - java.time.format.DateTimeParseException: could not be parsed at index 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM