英文:
JAVA : Convert hour into UTC+1 during Winter and UTC+2 during Summer
问题
我正在学习Java,尝试理解小时制的工作原理。
我使用的小时格式是HH:mm:ss
。
我想在冬季将这个字符串转换为UTC+1小时,在夏季转换为UTC+2小时。
为了知道日期,我使用的格式是:yyyy/MM/dd
。
以下是我使用的变量类型的示例。
有人可以帮助我解决这个问题吗?
String Hour = "14:12:13";
String Date = "2019/11/12";
英文:
I'm learning Java and I try to understand how the hour system work.
The hour format I use is HH:mm:ss
.
I want to convert this string into UTC+1 hour during winter and UTC+2 during summer.
To know the date, I use the format : yyyy/MM/dd
.
Below, an exemple of the kind of variable I use.
Can someone help me to resolve this problem ?
String Hour = "14:12:13";
String Date = "2019:11:12";
答案1
得分: 2
我使用的时间格式是HH:mm:ss
这是标准的ISO 8601格式。
java.time类在解析/生成文本时默认使用ISO 8601格式。
我想将这个字符串转换为冬季时区为UTC+1,夏季时区为UTC+2。
Java包含OffsetTime
类来表示具有与UTC的偏移的时间。但这个概念是有问题的。我的阅读和推理都无法理解一个没有日期的偏移时间。
我认为这个类只存在于与SQL规范定义的相同概念相匹配的情况下。根据我所知,这是没有意义的。SQL规范中不是唯一没有意义的东西。
要了解日期,我使用的格式是:yyyy/MM/dd。
对于数据交换、日志记录和调试,我建议您坚持使用ISO 8601格式,即YYYY-MM-DD。这与您的格式类似,但使用连字符而不是斜杠作为分隔符。
对于向用户展示数据,请让Java自动本地化,使用DateTimeFormatter.ofLocalized...
方法。没有必要为用户硬编码格式。
以下是我使用的一种变量的示例。
对于时间,使用LocalTime
。
对于日期,使用LocalDate
。
对于偏移,使用ZoneOffset
。
对于时区,使用ZoneId
。
对于瞬间,使用LocalDate
、LocalTime
和ZoneOffset
的组合来获得OffsetDateTime
。通常更好地将偏移替换为ZoneId
以获得ZonedDateTime
。
String timeString = "14:12:13";
String dateString = "2019-11-12";
LocalTime lt = LocalTime.parse(timeString);
LocalDate ld = LocalDate.parse(dateString);
ZoneId z = ZoneId.of("Africa/Tunis");
ZonedDateTime zdt = ZonedDateTime.of(ld, lt, z);
这些内容已经在Stack Overflow上多次讨论过。因此,我在这里简要介绍。搜索以获取更多信息。
英文:
> The hour format I use is HH:mm:ss
That is standard ISO 8601 format.
The java.time classes use ISO 8601 formats be default when parsing/generating text.
> I want to convert this string into UTC+1 hour during Winter and UTC+2 during Summer.
Java includes the OffsetTime
class to represent a time-of-day with an offset-from-UTC. But this concept is faulty. Both my reading and my reasoning fail to make sense of a time with offset yet lacking a date.
I believe this class exists only to match the same idea defined by the SQL spec. Again, senseless as far as I can tell. Not the only senseless thing in the SQL spec.
> To know the Date, i use the format : yyyy/MM/dd.
For data exchange, logging, and debugging, I suggest you stick with ISO 8601 format which is YYYY-MM-DD. That is like your format but using hyphen rather than slash as delimiter.
For presentation to the user, let Java automatically localize using DateTimeFormatter.ofLocalized…
methods. No point in hard-coding a format for your users.
> Behind, an exemple of the kind of variable i use.
For time-of-day, use LocalTime
.
For date, use LocalDate
.
For offset, use ZoneOffset
.
For time zone, use ZoneId
.
For a moment, use the combination of LocalDate
, LocalTime
, and ZoneOffset
to get a OffsetDateTime
. Generally better to switch out offset for ZoneId
to get a ZonedDateTime
.
> "14:12:13"; String Date = "2019:11:12"
LocalTime lt = LocalDate.parse( "14:12:13" ) ;
LocalDate ld = LocalDate.parse( "2019-11-12" ) ;
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
This has all been covered many many many times already on Stack Overflow. So, I am being brief here. Search to learn more.
答案2
得分: 0
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
String timeString = "14:12:13";
LocalTime time = LocalTime.parse(timeString);
System.out.println(time);
// Time at UTC+1
OffsetTime timeAtUTC1 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(1));
System.out.println(timeAtUTC1);
// Time at UTC+2
OffsetTime timeAtUTC2 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(2));
System.out.println(timeAtUTC2);
}
}
Output:
14:12:13
15:12:13+01:00
16:12:13+02:00
英文:
> I want to convert this string into UTC+1 hour during winter and UTC+2
> during summer.
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
String timeString = "14:12:13";
LocalTime time = LocalTime.parse(timeString);
System.out.println(time);
// Time at UTC+1
OffsetTime timeAtUTC1 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(1));
System.out.println(timeAtUTC1);
// Time at UTC+2
OffsetTime timeAtUTC2 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(2));
System.out.println(timeAtUTC2);
}
}
Output:
14:12:13
15:12:13+01:00
16:12:13+02:00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论