- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的代码中:
ZonedDateTime zdt = ZonedDateTime.now();
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String zdtString = FORMATTER.format(zdt);
System.out.println(zdtString);
您将看到它以 yyyy-DD-mm 格式打印出当前日期。由于这个问题是在 2021 年 7 月 17 日发布的,因此打印出:
2021-07-17
但现在我想将日期更改为不同的日期(例如 1994-03-24)。
所以我尝试了:
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ZonedDateTime zdt2 = ZonedDateTime.parse("1994-03-24", FORMATTER);
String zdtString = FORMATTER.format(zdt2);
System.out.println(zdtString);
但是我得到了以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '1994-03-24' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at javaapplication5.JavaApplication5.main(JavaApplication5.java:48)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
at java.time.ZoneId.from(ZoneId.java:466)
at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
... 4 more
如何将自己的日期设置为当前日期以外的日期?
最佳答案
1994-03-24 没有时区信息,因此在您提供时区信息之前无法将其解析为 ZonedDateTime
。此外,您还需要默认时间单位。
1994-03-24 可以直接解析为 LocalDate
,因为现代日期时间 API 基于 ISO 8601并且不需要显式使用 DateTimeFormatter
对象,只要日期时间字符串符合 ISO 8601 标准即可。
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDate.parse("1994-03-24"));
}
}
输出:
1994-03-24
使用默认时间单位和特定时区进行解析的演示:
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("u-M-d[ H]")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.toFormatter(Locale.ENGLISH)
.withZone(ZoneId.systemDefault());
ZonedDateTime zdt = ZonedDateTime.parse("1994-03-24", dtf);
System.out.println(zdt);
}
}
输出:
1994-03-24T00:00Z[Europe/London]
注意: ZoneId.systemDefault()
返回 JVM 的 ZoneId
。将其替换为适用的 ZoneId
例如ZoneId.of(“美国/纽约”)
。另外,请注意方括号内的可选模式,默认为 0。
从 Trail: Date Time 了解有关现代日期时间 API 的更多信息。
关于java - 如何在 Java 1.8 中设置 yyyy-MM-dd 格式的日期,然后能够以相同的格式打印出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68424630/
我需要使用正则表达式验证格式为 dd/mm/yyyy 的日期字符串。 此正则表达式验证 dd/mm/yyyy,但不验证 31/02/4500 等无效日期: ^(0?[1-9]|[12][0-9]|3[
我需要检查给定字符串的日期。我得到的字符串不规则,我不想使用 Dateformat 或类似的东西。我试图避免出现多个异常。我需要 dd.MM.yyyy 和 MM.yyyy 和 yyyy 的正则表达式。
我想使用在这里找到的 jQuery 屏蔽输入插件 http://digitalbush.com/projects/masked-input-plugin/格式化日期。 我正在从数据库中提取数据以在页面
我正在尝试以更有效的方式根据当前日期找出当前财政年度(FY - 3 月至 4 月)。这是我到目前为止写的内容 public static void main(String[] args) {
我已经搜索了该网站,但没有找到接近我需要的答案。基本上我的日期正确显示为“2013-05-09”,但我想要的是“2013 年 3 月 19 日”或使其更好“3 月 19 日, 2013 年”。 请向我
我需要使用 C# 更改日期字符串的格式 来自:“06/16/2010”或“16/06/2010” 至:“2010-06-16” 你能帮我实现这个吗 谢谢 最佳答案 如果您已经将它作为 DateTime
我正在以 MMM-yyyy 格式从 View 中提交日期值,即“Apr-2019”作为字符串。我需要将字符串值转换为 .Net 支持的日期格式,如 dd-MM-yyyy 或 yyyy-MM-dd。这里
我有一个包含以下数据的平面文件 2/12/2016 2/3/2017 12/23/2017 04/23/2017 第一个是 M/dd/yyyy,第二个是 M/d/yyyy,其余日期格式为 MM/dd/
好吧,我已经和这个战斗了足够长的时间。我有一个正则表达式 ^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d) 这将匹配 8/15、08/15
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Extending JavaScript's Date.parse to allow for DD/MM/Y
我有 json 对象列表,其中一个字段是日期。问题是日期在 json 中以不同的方式写入。 大多数看起来像: "publishedDate": "2005-01-28" "publishedDat
这个问题已经有答案了: Generate & parse "Year-Month" values in text from Java (1 个回答) 已关闭 7 年前。 我有一个变量: YearMon
我对日期格式有一个奇怪的问题。就像,我正在使用 jquery 日期选择器,它设置为格式 dd/mm/yy。因此,当我在该字段中填写 01/09/2015 时,它应该意味着日期是 2015 年 9 月
我有以下日期, 24/04/2019 13/05/2019 12/04/2019 我想删除年份并将这些日期转换为以下格式, 24/04 13/05 12/04 这不是仅用于输出我想在该日期执行加法或申
“YYYY”和“yyyy”之间的确切区别是什么。我读过 this link ,它指出 A common mistake is to use YYYY. yyyy specifies the calen
我有一个日期列,其中日期格式为 2010-04-14 在 SQL Server 中。有没有办法将日期格式检索为 14/04/2010 在选择语句中? 最佳答案 试试这个: SELECT CONVERT
我有这个正则表达式:(\d{4})\/(\d{2,4})\/?(\w{2})?这似乎工作正常,但我想避免以下情况,例如:我有一个字符串“2020/2000/WEEEE”并且它匹配它。 它匹配的其他错误
这是用户输入日期的代码:(它必须是我的日期选择器,但表单必须将日期字段作为文本提交 - 不要问) 提交时,我在 javascript 中调用验证逻辑。我附上了当我尝试输入 01/01/2001 作为用
我正在尝试以 YYYY-YY 格式提取会计日期字符串的最后一年 (YY)。例如,这个“1999-00”的最后一年是 2000 年。 当前代码似乎涵盖了除此之外的大多数情况。 import pandas
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Conversion of Date 我从对话框中获取的日期为:5-1-2012 - 作为字符串。 我需要将其转换为
我是一名优秀的程序员,十分优秀!