标签: datetimeformatter

如何将“2020-12-20T00:00:00.000Z”转换为java.util.Date?

我试着用

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默认时间戳?

java date datetime-parsing localdate datetimeformatter

1
推荐指数
1
解决办法
9022
查看次数

缺少 LocalDateTime.parse 中的第二个(00)

缺少第二个 (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)但结果仍然相同。

java java-time localdatetime datetimeformatter

1
推荐指数
1
解决办法
252
查看次数

无法在 Java 中将西班牙语日期字符串转换为 LocalDate

您好,我来这里是为了解决Locale格式化程序语言的DateTimeFormatter问题,让我向您介绍一下:

  • 我正在阅读具有以下字符串格式的 CSV 文件:“dd-MMM-yyyy”<-->“28-dic-2017”
  • 然后我必须将列表中的每个字符串解析为 LocalDate 以使用 timeAPI 方法处理每个字符串。
  • 请注意,我的月份是西班牙语缩写。那是一团糟!!
  • 执行时返回:

堆栈跟踪观察到的错误:

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 甚至不知道我在做什么,但我认为这显然不会起作用。

java datetime setlocale java-8 datetimeformatter

1
推荐指数
1
解决办法
201
查看次数

如何在 Java 中将日期/时间/区域格式化为与 UTC 的偏移量模式?

我有时间戳。例如:

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 utc datetimeformatter

1
推荐指数
1
解决办法
611
查看次数

DateTimeFormatter 得到 LocalDateTime.withYear(0) 的令人困惑的结果?

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)

java datetimeformatter

1
推荐指数
1
解决办法
45
查看次数

来自 yyyy-mm-dd 中日期字符串的 ZonedDateTime

尝试从日期字符串(例如“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)

java date-format java-time zoneddatetime datetimeformatter

0
推荐指数
1
解决办法
4838
查看次数

Java ZonedDateTime.format 问题

运行到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)

我能够重现:

  • MacOS HotSpot Java 10(Java 版本“10.0.2”2018-07-17)
  • OpenJDK 版本“11.0.9.1” 2020-11-04

如果您对 Lisps 没问题 – 您可以在此处的云 REPL 中亲自查看

java zoneddatetime datetimeformatter

0
推荐指数
1
解决办法
116
查看次数

如何将 LocalDateTime yyyy-MM-dd HH:mm 格式化为 dd-MM-yyyy HH:mm 作为字符串,以获取不仅包含日期属性的对象的整个列表?

我正在尝试解决一个似乎很棘手但我陷入困境的问题。你能帮忙吗?

在 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 date spring-boot java-stream datetimeformatter

0
推荐指数
1
解决办法
1008
查看次数