如何在不使用格式化字符串文字的情况下保证 Java 中的 2 位数月份?
How to guarantee 2 digit month in Java without using formatting string literals?
我不确定这是否会始终发出 2 位数的月份:
java.time.LocalDateTime ldt = java.time.LocalDateTime.now( java.time.ZoneId.of( "America/New_York" ));
String path = "my/path/" + ldt.getYear().toString() + "/" + ldt.getMonthValue().toString() + "/" + ldt.getDayOfMonth().toString() + "/" + ldt.getHour().toString() + "/";
现在可以了,因为我们处于两位数的月份。例如,如何检查它是否适用于 7 月?
无法使用“%02d”格式。
抱歉,刚接触 Java。
tl;博士
正在将您的请求切换为使用标准 ISO 8601 格式...
"my/path/" +
ZonedDateTime.now(
ZoneId.of( "America/New_York" )
)
.format(
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH" )
)
.toString()
my/path/2020-11-17T21
DateTimeFormatter
让 java.time.format.DateTimeFormatter
class 完成您的格式化工作。无需您操作字符串。
dd
而不是 d
这样的双字符在需要的地方强制使用前导零。仔细阅读 class Javadoc 以了解格式化代码。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd/HH" ) ;
String s = ldt.format( f ) ;
ISO 8601
您的斜杠字符 (SOLIDUS) 可以解释为 folder/directory。所以我建议您坚持使用标准化 ISO 8601 格式。标准格式保持年-月-日-时的顺序,但使用 HYPHEN 字符作为分隔符。 T
将一天中的时间与日期部分分开。
java.timeclass默认使用 ISO 8601 格式。因此无需指定任何格式模式。
当你使用它时,我也会使用分钟,这样日期和时间就很容易识别了。如果你想要始终如一的零分钟,truncate 到小时。
String s = ldt.truncatedTo( java.time.temporal.ChronoUnit.HOURS ).toString() ;
上一期:在一些流行的文件系统中,冒号字符也可能被解释为folder/directory。所以我们应该避免在小时和分钟之间使用 COLON 字符。请参阅 this Question at sibling site http://www.AskDifferent.com/ 关于此的策略。
或者您可能只想坚持使用小时而不使用分钟。
综合起来:
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH" ) ;
ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalDateTime ldt = LocalDateTime.now( z ) ;
String s = ldt.format( f ) ;
String output = "my/path/" + s ;
System.out.println( "output → " + output ) ;
看到这个code run live at IdeOne.com。
output → my/path/2020-11-17T21
LocalDateTime
不是片刻
对了,你明白LocalDateTime
是不是代表一个时刻吗?它故意缺少任何时区或与 UTC 的偏移量的概念。该类型表示诸如 2021 年 1 月 23 日之类的日期和诸如中午之类的时间,但我们不知道这意味着东京 23 日中午、巴黎 23 日中午还是 23 日中午在蒙特利尔,相隔几个小时的三个截然不同的时刻。所以您的代码 java.time.LocalDateTime.now( java.time.ZoneId.of( "America/New_York" ))
可能不符合您的期望。
要捕捉瞬间,请使用 Instant
(始终采用 UTC)、OffsetDateTime
或 ZonedDateTime
。
我不确定这是否会始终发出 2 位数的月份:
java.time.LocalDateTime ldt = java.time.LocalDateTime.now( java.time.ZoneId.of( "America/New_York" ));
String path = "my/path/" + ldt.getYear().toString() + "/" + ldt.getMonthValue().toString() + "/" + ldt.getDayOfMonth().toString() + "/" + ldt.getHour().toString() + "/";
现在可以了,因为我们处于两位数的月份。例如,如何检查它是否适用于 7 月?
无法使用“%02d”格式。
抱歉,刚接触 Java。
tl;博士
正在将您的请求切换为使用标准 ISO 8601 格式...
"my/path/" +
ZonedDateTime.now(
ZoneId.of( "America/New_York" )
)
.format(
DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH" )
)
.toString()
my/path/2020-11-17T21
DateTimeFormatter
让 java.time.format.DateTimeFormatter
class 完成您的格式化工作。无需您操作字符串。
dd
而不是 d
这样的双字符在需要的地方强制使用前导零。仔细阅读 class Javadoc 以了解格式化代码。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd/HH" ) ;
String s = ldt.format( f ) ;
ISO 8601
您的斜杠字符 (SOLIDUS) 可以解释为 folder/directory。所以我建议您坚持使用标准化 ISO 8601 格式。标准格式保持年-月-日-时的顺序,但使用 HYPHEN 字符作为分隔符。 T
将一天中的时间与日期部分分开。
java.timeclass默认使用 ISO 8601 格式。因此无需指定任何格式模式。
当你使用它时,我也会使用分钟,这样日期和时间就很容易识别了。如果你想要始终如一的零分钟,truncate 到小时。
String s = ldt.truncatedTo( java.time.temporal.ChronoUnit.HOURS ).toString() ;
上一期:在一些流行的文件系统中,冒号字符也可能被解释为folder/directory。所以我们应该避免在小时和分钟之间使用 COLON 字符。请参阅 this Question at sibling site http://www.AskDifferent.com/ 关于此的策略。
或者您可能只想坚持使用小时而不使用分钟。
综合起来:
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH" ) ;
ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalDateTime ldt = LocalDateTime.now( z ) ;
String s = ldt.format( f ) ;
String output = "my/path/" + s ;
System.out.println( "output → " + output ) ;
看到这个code run live at IdeOne.com。
output → my/path/2020-11-17T21
LocalDateTime
不是片刻
对了,你明白LocalDateTime
是不是代表一个时刻吗?它故意缺少任何时区或与 UTC 的偏移量的概念。该类型表示诸如 2021 年 1 月 23 日之类的日期和诸如中午之类的时间,但我们不知道这意味着东京 23 日中午、巴黎 23 日中午还是 23 日中午在蒙特利尔,相隔几个小时的三个截然不同的时刻。所以您的代码 java.time.LocalDateTime.now( java.time.ZoneId.of( "America/New_York" ))
可能不符合您的期望。
要捕捉瞬间,请使用 Instant
(始终采用 UTC)、OffsetDateTime
或 ZonedDateTime
。