简体   繁体   English

为包含00:00:00的字符串添加小时数

[英]Adding hours to a String containing 00:00:00

I am going to send the dateTime variable to an API which requires a String with the time and date in it. 我将把dateTime变量发送到一个API,该API需要一个包含时间和日期的String。 But I noticed that there is a time difference, because when I send that variable, it ends up with two hours added on it. 但是我注意到存在时间差异,因为当我发送该变量时,最终会增加两个小时。 So I need to add 2 hours to the time variable. 所以我需要在时间变量上加2小时。

I tried splitting the time variable, parsed the first element to an int and added 2, and then formatted to a String and so forth, but this seems extremely unnecessarily long. 我尝试拆分时间变量,将第一个元素解析为int并添加2,然后格式化为String等等,但这看起来非常不必要。 Is there any easier way? 有没有更简单的方法? I've tried to figure out how to work with time objects and setting the date and time, but I only know how to set to the current time, which I do not want to do. 我试图找出如何处理时间对象和设置日期和时间,但我只知道如何设置当前时间,我不想这样做。 The code below contains the values needed. 下面的代码包含所需的值。

String date = "2019-05-09";
String time = "10:00:00";
String dateTime = (date + "T" + time + "Z");

Is there any easier way? 有没有更简单的方法?

Yes, use the modern java.time classes. 是的,使用现代java.time类。

Apparently you are trying to communicate a moment of 10 AM on that date in UTC. 显然,您正试图在UTC的那个日期上午10点进行通信。 The Z in your example code means UTC, pronounced “Zulu”. 示例代码中的Z表示UTC,发音为“Zulu”。

To do so, use OffsetDateTime with an offset specified by the ZoneOffset.UTC constant. 为此,请使用OffsetDateTimeZoneOffset.UTC常量指定的偏移量。

LocalDate ld = LocalDate.parse( "2019-05-09" ) ;
LocalTime lt = LocalTime.parse( "10:00:00" ) ;
OffsetDateTime odt = OffsetDateTime.of( ld , lt , ZoneOffset.UTC ) ;

Apparently you want to send this value as a string in standard ISO 8601 format. 显然,您希望将此值作为标准ISO 8601格式的字符串发送。 Those formats are used by default in the java.time classes when parsing/generating strings. 在解析/生成字符串时,默认情况下在java.time类中使用这些格式。

String output = odt.toString() ;

You do not explain exactly how or where this 2 hour discrepancy appears. 您没有准确解释这2小时差异的显示方式和位置。 I would guess that you are seeing a result after an adjustment has been made from UTC to some time zone. 在从UTC到某个时区进行调整后,我猜你会看到一个结果。 If so, you have no problem . 如果是这样, 你没有问题

If two people are telephoning, in time zones two hours apart, if they look up simultaneously to read the current time from the clock on their respective wall, one person will see 10 AM while the other sees 12 noon. 如果两个人打电话,在相隔两个小时的时区,如果他们同时抬头从各自的墙上的时钟读取当前时间,一个人将看到上午10点,而另一个人看到中午12点。 Same moment, same point on the timeline, different wall-clock time. 同一时刻,时间轴上的同一点,不同的挂钟时间。

You can verify this by applying a ZoneId for the suspected time zone, to see the wall-clock time value in the resulting ZonedDateTime object. 您可以通过对可疑时区应用ZoneId来验证这一点,以查看生成的ZonedDateTime对象中的挂钟时间值。

ZoneId z = ZoneId.of( "Europe/Paris" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;  // Same moment, same point on the timeline, different wall-click time. 

This method might give you helpful hints. 此方法可能会为您提供有用的提示。 Output is: 2019-05-09 16:05 输出为:2019-05-09 16:05

public static void main(String[] args) {

LocalDateTime localDateTime = LocalDateTime.now().plusHours(2);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(localDateTime.format(formatter));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM