从https://api.spacexdata.com/v3/launches获取日期 该日期的格式为:2006-03-25T10:30:00+12:00。我想将其转换为“dd,mm,yyyy”,但总是收到错误:“java.time.format.DateTimeParseException:文本'2006-03-25T10:30:00 + 12:00'无法解析,未解析的文本发现于索引 10"
我的代码:
val formatter = DateTimeFormatter.ofPattern("dd, mm, yyyy", Locale.US)
val myDate = LocalDate.parse(launchDate, formatter)
var launchDateConverted: String= myDate.toString()
Run Code Online (Sandbox Code Playgroud)
我在字符串中获取数据,然后将其转换为日期进行格式化,然后将日期转换回字符串以在 UI 上显示。我使用了不同的方法,但找不到正确的方法。我当前的语言环境是“RU”。
我有一个使用 Thymeleaf 作为视图层的 Spring Boot Web 应用程序,我需要显示一些具有特定时区的日期 (CET = UTC+1/UTC+2)。
服务器设置为 UTC,所有日期也以 UTC 格式作为日期时间存储在我的数据库中,这很好。
现在我想使用 Thymeleaf Temporals 在 HTML 页面上显示此日期,不是以 UTC 格式,而是以 CET 格式,但它似乎不起作用。
日期对象是一个 Java Instant。
从数据库检索的日期是(例如)2021-02-17T16:18:21Z并且显示如下:
<div th:text="${#temporals.format(user.lastAccess, 'dd/MM/yyyy HH:mm')}"></div>
=> 17/02/2021 16:18
Run Code Online (Sandbox Code Playgroud)
但我想像这样展示它:
17/02/2021 17:18
Run Code Online (Sandbox Code Playgroud)
所以我用了:
或者
<div th:text="${#temporals.format(user.lastAccess, 'dd/MM/yyyy HH:mm', new java.util.Locale('it', 'IT'))}"></div>
Run Code Online (Sandbox Code Playgroud)
但日期始终显示为 UTC
17/02/2021 16:18
Run Code Online (Sandbox Code Playgroud)
Thymeleaf 的配置Java8TimeDialect正确。
我在用着:
Spring Boot 2.2.4
Thymeleaf 3.0.11.RELEASE
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
谢谢。
如何从字符串中解析 LocalTime,例如 mm:ss.S 格式的“10:38.0”?我很难改变格式。
public static LocalTime parseTime(String time) {
return localTime = LocalTime.parse(time, DateTimeFormatter.ofPattern("mm:ss.S"));
}
Run Code Online (Sandbox Code Playgroud)
出现错误
java.time.format.Parsed java.time.format.DateTimeParseException 类型的 ISO:无法解析文本 '10:38.2':无法从 TemporalAccessor 获取 LocalTime:{MinuteOfHour=10,MicroOfSecond=200000,MilliOfSecond=200,NanoOfSecond =200000000,秒数=38},
在下面的输出部分中,显示的“中间时间”为零。预期结果是一个非零值。
Java代码:
ZonedDateTime zdt1 = ZonedDateTime.now(ZoneId.of("Asia/Calcutta"));
ZonedDateTime zdt2 = ZonedDateTime.now(ZoneId.of("America/Toronto"));
Duration duration2 = Duration.between(zdt1, zdt2);
System.out.format("ZonedDateTime 1 %s\n", zdt1);
System.out.format("ZonedDateTime 2 %s\n", zdt2);
System.out.format("Hours in between %d\n", duration2.toHours());
Run Code Online (Sandbox Code Playgroud)
输出:
ZonedDateTime 1 2021-11-10T16:44:38.237844+05:30[Asia/Calcutta]
ZonedDateTime 2 2021-11-10T06:14:38.239340-05:00[America/Toronto]
Hours in between 0
Run Code Online (Sandbox Code Playgroud) 我的代码中有以下行java,我试图格式化日期字符串
ZonedDateTime zonedDateTime= ZonedDateTime.ofInstant(instant, tz);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm aa");
return zonedDateTime.format(formatter);
Run Code Online (Sandbox Code Playgroud)
在第 2 行,我收到此错误:
java.lang.IllegalArgumentException: Too many pattern letters: a
at java.time.format.DateTimeFormatterBuilder.parseField(DateTimeFormatterBuilder.java:1774)
Run Code Online (Sandbox Code Playgroud)
我如何创建一个格式字符串,DateTimeFormatter.ofPattern例如:
2023-04-21 7.00pm
Run Code Online (Sandbox Code Playgroud) 考虑代码:
public static void main(String[] args) {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH")));
}
Run Code Online (Sandbox Code Playgroud)
输出为2022-05-04-17UTC 时间2022-05-04-10。文档说这LocalDateTime是没有区域偏移的 - 那么为什么会有 7 小时轮班呢?(我当地的时区是+7UTC)
Instant我正在尝试根据本文中的参考从对象创建 ISO 8601 格式的 DateTime,我使用该格式YYYY-MM-DD'T'hh:mm:ss'T'ZD来解析即时日期,如下所示。
但它生成的时间格式错误:
2022-06-172T06:08:13T-0500172
Run Code Online (Sandbox Code Playgroud)
预期的格式应该是:
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud)
我的代码:
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("YYYY-MM-DD'T'hh:mm:ss'T'ZD")
.withZone(ZoneId.systemDefault());
formatter.format(Instant.now())
Run Code Online (Sandbox Code Playgroud)
如何生成如下所示的格式化时间?
2022-06-21T13:31:49-05:00
Run Code Online (Sandbox Code Playgroud) 我已经编写了下面的代码,但如果当前日期时间是2022-07-03 09:48:05.448并且我添加30 分钟,我的响应将返回2022-07-03 09:79:05.448。
但分钟永远不可能是 79,它应该改为小时......
public static String getExpiryDate(int additionalMinutesToCurrentMinute) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute() + additionalMinutesToCurrentMinute;
int second = now.getSecond();
int millis = now.get(ChronoField.MILLI_OF_SECOND); // Note: no direct getter available.
String expiryDateAndTime = String.format("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis);
return expiryDateAndTime;
}
Run Code Online (Sandbox Code Playgroud) 我正在将毫秒转换为 ZonedDateTime
Long lEpochMilliSeconds = 1668415926445;
System.out.println(ZonedDateTime.ofInstant(Instant.ofEpochMilli(lEpochMilliSeconds),ZoneId.of("UTC"));
Run Code Online (Sandbox Code Playgroud)
它给出输出:
2022-10-28T12:59:34.939Z[UTC]
Run Code Online (Sandbox Code Playgroud)
我不希望输出中包含时区“[UTC]”部分。我需要我的输出是这样的 ZonedDateTime 格式:
2022-10-28T12:59:34.939Z
Run Code Online (Sandbox Code Playgroud)
我只需要ZonedDateTime中的格式而不是 string,因为我将返回该值并在其他地方使用它
我希望用户输入 Local.Time 从 00 到 23 和从 00 到 59 的小时和分钟,我将其扫描为整数。它可以工作,但对于从 00 到 09 的值,int 会忽略 0 并将其放置为 0,1,2...9 而不是 00,01,02,03...09;这会破坏 Local.Time,例如“10:3”;不是有效的时间格式。
我读过我可以将其格式化为字符串,但我认为这对我没有帮助,因为我需要一个 int 值来构建 LocalTime 以及随后的操作。
有一种方法可以在将变量保留为 int 的同时对其进行格式化?我可以用不同的方式编码来绕过这个吗?我对这些课程的运作方式有误吗?
我对这些概念很陌生
这是我正在使用的代码
int hours;
int minutes;
System.out.println("Input a number for the hours (00-23): ");
hours = scan.nextInt();
System.out.println("Input a number for the minutes (00-59): ");
minutes = scan.nextInt();
LocalTime result = LocalTime.parse(hours + ":" + minutes);
Run Code Online (Sandbox Code Playgroud)
我尝试使用 NumberFormat 类,但在尝试声明其变量时返回错误(比如它是一个抽象变量,无法实例化)
我也尝试使用字符串格式,但我真的不知道之后如何处理该字符串,它要求我提供一个 int 而不是字符串来构建此方法