我试着用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(d.toString(), formatter);
Run Code Online (Sandbox Code Playgroud)
但它会引发错误。
有没有办法转换JSON默认时间戳?
缺少第二个 (00) 从 LocalDateTime.parse
LocalTime time = LocalTime.NOON;
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
String value ="20200810" + time.format(formatTime);
LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
日志
LocalTime time = LocalTime.NOON;
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
String value ="20200810" + time.format(formatTime);
LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"));
Run Code Online (Sandbox Code Playgroud)
我也尝试更改LocalTime.NOON为,LocalTime.of(12,0,0)但结果仍然相同。
您好,我来这里是为了解决Locale格式化程序语言的DateTimeFormatter问题,让我向您介绍一下:
堆栈跟踪观察到的错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '28-dic-2017' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953)
at java.base/java.time.LocalDate.parse(LocalDate.java:429)
at com.examen.Presentation.Principal.parseDate(Principal.java:28)
at com.examen.Presentation.Principal.main(Principal.java:19)
Run Code Online (Sandbox Code Playgroud)
最小的、可重现的代码示例:
public static LocalDate parseDate(String fecha) {
Locale loc = new Locale("es", "ES");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", loc);
LocalDate date = LocalDate.parse(fecha, formatter);
return date;
}
Run Code Online (Sandbox Code Playgroud)
我在尝试来自语言环境的不同事物和方法时遇到了同样的错误,而 DateTimeFormatter 甚至不知道我在做什么,但我认为这显然不会起作用。
我有时间戳。例如:
OffsetDateTime timestamp = OffsetDateTime.parse("2022-12-23T12:00:00.000+02:00");
Run Code Online (Sandbox Code Playgroud)
如何通过 DateTimeFormatter 对其进行格式化?我找不到我需要的格式模式:
"23.12.2022 at 12:00 (UTC+2)"
Run Code Online (Sandbox Code Playgroud)
并且无法创建新的,因为当我使用时:
"23.12.2022 at 12:00 (UTC+2)"
Run Code Online (Sandbox Code Playgroud)
然后我得到 UTC+02,但我们需要 UTC+2。
附加问题:我们可以通过模式获取单词UTC吗?
Java版本:1.8.0_202
见下面的代码:
DateTimeFormatter yy = DateTimeFormatter.ofPattern("yy");
DateTimeFormatter yyy = new DateTimeFormatterBuilder()
.appendValueReduced(ChronoField.YEAR, 3, 3, 0)
.toFormatter();
DateTimeFormatter yyyy = DateTimeFormatter.ofPattern("yyyy");
LocalDateTime now = LocalDateTime.now();
// set year is 0
LocalDateTime year0 = now.withYear(0);
System.out.println("year0: " + year0); // year0: 0000-01-10T09:53:03.551
System.out.println("year0.getYear(): \t" + year0.getYear()); // year0.getYear(): 0
System.out.println("year0.format(yyyy): " + year0.format(yyyy)); // year0.format(yyyy): 0001
System.out.println("year0.format(yyy): \t" + year0.format(yyy)); // year0.format(yyy): 000
System.out.println("year0.format(yy): \t" + year0.format(yy)); // year0.format(yy): 01
System.out.println("============================================================");
// set year is 1
LocalDateTime year1 = now.withYear(1);
System.out.println("year1: …Run Code Online (Sandbox Code Playgroud) 尝试从日期字符串(例如“2020-08-24”)解析 ZonedDateTime。
使用 TemporalAccesor 和 DateTimeFormatter.ISO_OFFSET_DATE 进行解析时,我收到 java.time.format.DateTimeParseException。我使用了错误的格式化程序吗?
甚至尝试在日期字符串末尾添加“Z”,以便将其理解为 UTC
private ZonedDateTime getZonedDateTime(String dateString) {
TemporalAccessor parsed = null;
dateString = dateString + 'Z';
try {
parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse(dateString);
} catch (Exception e) {
log.error("Unable to parse date {} using formatter DateTimeFormatter.ISO_INSTANT", dateString);
}
return ZonedDateTime.from(parsed);
}
Run Code Online (Sandbox Code Playgroud) 运行到ZonedDateTime格式化的问题。它将 2020 年 12 月的结束日期格式化为 2021 年的日期。Java 片段
ZonedDateTime z1 = ZonedDateTime.of(LocalDateTime.of(2020, 12, 31, 0, 0), ZoneId.of("America/New_York"));
z1.format(DateTimeFormatter.ofPattern("YYYY-MM-dd"))
// yields "2021-12-31"
Run Code Online (Sandbox Code Playgroud)
12 月 28 日、29 日、30 日也是如此。不同时区的结果相同。重复 2019 年 12 月 30 日(格式为 2020-12-30)。
原始 Clojure 片段
(let [zdt (ZonedDateTime/of (LocalDateTime/of 2020 12 31 0 0) (ZoneId/of "America/New_York"))
f (DateTimeFormatter/ofPattern "YYYY-MM-dd")]
(.format zdt f))
; => "2021-12-31"
Run Code Online (Sandbox Code Playgroud)
我能够重现:
我正在尝试解决一个似乎很棘手但我陷入困境的问题。你能帮忙吗?
在 Spring Boot 中,我有一个Holiday保存在 MySQL 数据库上的国定假日对象。该对象Holiday包含以下属性:
(...)
@Column(name = "title")
private String HolidayTitle;
@Column(name = "date")
private String holidayDate;
@Column(name = "updated")
private LocalDateTime updated;
@Column(name = "country")
private String country;
(...)
Run Code Online (Sandbox Code Playgroud)
但我对该属性感兴趣updated,该属性在数据库中存储为 2021-10-26 20:54:48。我想将其格式化为 Thymeleaf 的 dd-MM-yyyy HH:mm,但它由具有不同属性的对象列表组成Holiday。
在控制器中,列表被检索如下:
@GetMapping("/getAllHolidays")
public String getAllHolidays(Model theModel) {
List<Holiday> theHolidays = holidayService.findAll();
theModel.addAttribute("holidays", theHolidays);
return "holidays";
}
Run Code Online (Sandbox Code Playgroud)
如何使用仅格式化updated列表中所有假日对象的属性?theHolidaysDateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
Run Code Online (Sandbox Code Playgroud)
是否可以使用Java流来实现这一点?
java ×8
date ×2
java-time ×2
date-format ×1
datetime ×1
java-8 ×1
java-stream ×1
localdate ×1
setlocale ×1
spring-boot ×1
utc ×1