USQ*_*Q91 0 java date-format java-time zoneddatetime datetimeformatter
尝试从日期字符串(例如“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)
LocalDate#atStartOfDay请按如下方式进行:
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Define a DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
// Given date string
String strDate = "2020-08-24";
// Parse the given date string to ZonedDateTime
ZonedDateTime zdt = LocalDate.parse(strDate, dtf).atStartOfDay(ZoneOffset.UTC);
System.out.println(zdt);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
2020-08-24T00:00Z
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4838 次 |
| 最近记录: |