英文:
How to add an hour and set the timezone to "UTC" in java?
问题
我想将一个小时加到当前时间,并获得UTC时间。我尝试使用Calendar类,但它只增加了小时,没有给出UTC时区的日期;另一方面,SimpleDateFormat设置了时区,但没有增加一个小时。该怎么做?
public static void main(String[] args)
{
Date currentDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 1);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateOfDeletion = simpleDateFormat.format(calendar.getTime());
System.out.println(currentDate);
System.out.println(dateOfDeletion);
System.out.print(calendar.getTime());
}
输出:
Mon Jun 26 14:05:56 IST 2023
Mon Jun 26 15:05:56 UTC 2023
Mon Jun 26 15:05:56 IST 2023
我希望日期在UTC时区,并在当前时间的基础上增加1小时。
英文:
I want to increment an hour to the current time and also get the date in UTC. I tried to use Calender class but that only increments the hour but doesn't give the Date in UTC timezone, on the other hand, SimpleDateFormat sets the timezone but doesn't increment an hour. How to do it?
public static void main(String[] args)
{
Date currentDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 1);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
simpleDateFormat.format(calendar.get(Calendar.HOUR));
String dateOfDeletion = simpleDateFormat.format(new Date()).toString();
System.out.println(currentDate) ;
System.out.println(dateOfDeletion);
System.out.print(calendar.getTime());
}
Output:
Mon Jun 26 14:05:56 IST 2023
Mon Jun 26 08:35:56 UTC 2023
Mon Jun 26 15:05:56 IST 2023
I want the date to be in UTC with 1 hour incremented from the current time.
答案1
得分: 2
尝试使用 ZonedTimeDate
:
final var dateOfDeletion = ZonedDateTime.now()
.plusHours(1)
.withZoneSameInstant(ZoneId.of("UTC"));
now()
会给你当前的时间,plusHours()
将其移动指定的小时数,而 withZoneSameInstant()
则会改变时区,但保持时间(瞬间)不变。
最初我使用了 withZoneSameLocal()
,但那只是将原始时区替换为指定时区,而不会调整时间(瞬间),在大多数情况下这不是你想要的结果。
如果你真的需要一个 java.util.Date
实例,你可以转换 dateOfDeletion
,但这意味着你会再次失去时区信息。
如果你只需要日期作为字符串,dateOfDeletion.toString()
已经可以返回 ISO 8601 格式的日期。其他格式可以通过 DateTimeFormatter
实例来实现。
请参考 java.time 包的文档。
根据评论,我了解到PO只想要一个提前一小时并且以UTC时间提供的 java.util.Date
。
在内部,java.util.Date
实例只是一个围绕表示自纪元开始(1970-01-01T00:00:00.000 UTC)的毫秒数的薄包装器。因此,Date::getTime
已经返回了UTC时间。要获取调整后的 Date
实例,请尝试以下代码:
final var dateOfDeletion = new Date(new Date().getTime() + 3_600_000);
但使用除默认时区以外的时区来打印 Date
实例是有挑战性的...
你可以将其转换为 Calendar
实例并打印:
final var calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTime(dateOfDeletion);
System.out.printf("%tDT%<tT %<tZ%n", calendar);
但仍然需要注意,你的 Date
没有时区信息...
英文:
Try ZonedTimeDate
:
final var dateOfDeletion = ZonedDateTime.now()
.plusHours( 1 )
.withZoneSameInstant( ZoneId.of( "UTC" ) );
now()
gives you the current time, plusHours()
moves that by the given number of hours, and withZoneSameInstant()
changes the time zone, by keeping the time (the instant).
Originally, I used withZoneSameLocal()
, but that just replaces the original time zone with the given one, without adjusting the time (the instant); in most cases this is not what you want.
If you really need an instance of java.util.Date
you can convert dateOfDeletion
, but that means you will lose the time zone again.
If you just need the date as a string, dateOfDeletion.toString()
already does the job for an ISO 8601 format. Other formats are possible through a DateTimeFormatter
instance.
Refer to the documentation of the java.time
package.
From the comments, I understood that the PO just want to have a java.util.Date
adjusted by one hour, provided in UTC.
Internally, an instance of java.util.Date
is just a thin wrapper around a long
representing the milliseconds since the begin of the epoch (1970-01-01T00:00:00.000 UTC). So Date::getTime
returns this time, already for UTC. To get the adjusted Date
instance, try this:
final var dateOfDeletion = new Date( new Date().getTime() + 3_600_000 );
But printing a Date
instance with a time zone other than the default one is a challenge …
You can convert it to a Calendar
instance and print that:
final var calendar = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
calendar.setTime( dateOfDeletion );
System.out.printf("%tDT%<tT %<tZ%n", calendar );
But still your Date
has no time zone …
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论