[英]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 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.查看文档页面以了解有关这些符号的更多信息。
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
指定时代的年份(时代指定为AD
或BC
)并且始终为正数,而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
(大多数情况下), y
和u
都会给您相同的数字。 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 的更多信息。
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.