我需要以下格式的当前日期:00:00 CST时间。
比如说,现在的日期是2021年7月9日,那么我需要那个日期和00:00的时间。
预期输出:
Fri Jul 09 00:00:00 CST 2021
当前代码:
LocalDateTime date= LocalDateTime.now().with(LocalTime.MIN);
Date dayStart= Date.from(date.atZone(ZoneId.systemDefault()).toInstant());输出的CurrentCode: Fri Jul 09 00:00:00 UTC 2021
当前代码选择服务器时/GMT,比CST提前5小时。
示例: 09AM GMT is 14 CST/2PM CST
我试着把日历设置为00:00,setTimeZone到美国/纽约。但是,在以calendar.getTime()的形式返回输出时,给出一些其他的时间,而不是给CST 00:00:00。
尝试过: @scheduled(zone="America/New_york" ) --不起作用。
发布于 2021-07-09 17:58:50
有两个好的答案。我的建议只是在细节上有所不同(据一些人说,这就是魔鬼所在的地方)。
首先,如果可以的话,您应该避免使用Date类。您已经在使用java.time,现代java.time和time (LocalDateTime、LocalTime、ZoneId)。您正在从java.time获得所需的所有功能。因此,我假设您只是在转换为Date,因为您需要一个Date,用于一些您目前无法升级到java.time的遗留API。
Instant startOfDay = ZonedDateTime.now(ZoneId.systemDefault())
.truncatedTo(ChronoUnit.DAYS)
.toInstant();
Date oldfashionedDate = Date.from(startOfDay);
System.out.println(oldfashionedDate);.truncatedTo(ChronoUnit.DAYS)丢弃了ZonedDateTime的时间部分,通常在同一天的同一时区给我们留下00:00:00。
编辑:
我想要美国芝加哥时间CDT
运行上述代码时,JVM的默认时区设置为America/Chicago,给出:
星期一7月12日00:00:00 CDT 2021
获得正确的Date值和获得预期的打印输出是两件不同的事情,因为Date.toString()选择JVM的默认时区并使用它来呈现要返回的字符串。这种行为可能会给人带来惊喜。无法说服Date.toString()使用不同的时区,因此上述输出只能通过设置默认时区来实现。
原文:有很多时区被称为CST,我无法确定你想要哪一个时区(我相信说美国/纽约不是其中之一的评论是正确的)。然而,我唯一能说服老式的Date类缩写CST的是中国标准时间。在亚洲/上海时区运行上述代码提供了以下输出:
星期六7月10日: 00:00:00 CST 2021
在我看来是对的。自从你问了问题,现在已经是中国标准时区星期六十号了。
任何时区都可以获得正确的Date值,即该时区中一天的开始。您还希望在打印输出中使用CST的缩写。不适用于此的时区包括:
Sat Jul 10 00:00:00 ACST 2021。因此,即使澳大利亚中央标准时间有时被简称为CST,Date还是选择了Date古巴在一年的这个时候使用古巴日照时间,所以CDT是output.无论如何,你知道你想要的时区,所以我要留给你来挑选。
发布于 2021-07-09 13:46:02
更改时区最简单的方法是使用System.setProperty("user.timezone"," America/New_York ");在Java中::America/纽约不是CST。
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
class DateAndTimeZoneFormatter {
public static void main(String[] args) {
System.setProperty("user.timezone", "America/New_York");
LocalDateTime date = LocalDateTime.now().with(LocalTime.MIN);
Date dayStart = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
System.out.println(dayStart);
}
}发布于 2021-07-09 14:10:33
您可以尝试在特定的ZonedDateTime中使用ZoneId。
public static void main(String[] args) {
// define the zone to be used
ZoneId americaChicago = ZoneId.of("America/Chicago");
// then take the date of today
ZonedDateTime nowInChicago = ZonedDateTime.of(
LocalDate.now(), // today
LocalTime.MIN, // start of day
americaChicago // zone
);
// define an output format
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("EEE MMM dd HH:mm:ss")
.appendLiteral(' ')
.appendZoneText(TextStyle.SHORT)
.appendLiteral(' ')
.appendPattern("uuuu")
.toFormatter(Locale.ENGLISH);
// then print the formatted String representation
System.out.println(nowInChicago.format(dtf));
}这输出
Fri Jul 09 00:00:00 CDT 2021缺点:
CST没有被应用,因为这是夏令时。因此,CDT将是输出。Date.from --此ZonedDateTime.toInstant()将影响值:应用这两条线
Date sameNowInChicago = Date.from(nowInChicago.toInstant());
System.out.println(sameNowInChicago);威尔输出
Fri Jul 09 07:00:00 CEST 2021在我的机器上,因为它被配置为有一个ZoneId.of("Europe/Berlin"),在打印java.util.Date时将考虑到这一点。
⇒中欧夏季时间。
https://stackoverflow.com/questions/68317067
复制相似问题