针对将UTC时间戳转换为Java中的偏移量时间和javaunix时间戳转换这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c#–使用Nodatime将UTC时间转换为本地时间、datetim
针对将UTC时间戳转换为Java中的偏移量时间和java unix时间戳转换这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c# – 使用Nodatime将UTC时间转换为本地时间、datetime – 将unix时间戳转换为julian、Java 8将给定的时间和时区转换为UTC时间、java – 将时间戳转换为UTC时区等相关知识,希望可以帮助到你。
本文目录一览:- 将UTC时间戳转换为Java中的偏移量时间(java unix时间戳转换)
- c# – 使用Nodatime将UTC时间转换为本地时间
- datetime – 将unix时间戳转换为julian
- Java 8将给定的时间和时区转换为UTC时间
- java – 将时间戳转换为UTC时区
将UTC时间戳转换为Java中的偏移量时间(java unix时间戳转换)
如何解决将UTC时间戳转换为Java中的偏移量时间?
我有一个 MysqL UTC 时间戳“2021-07-23 08:13:17”。
我想将其转换为具有偏移量的时间,例如“2021-07-21T08:13:17+01:00”。我的代码是:
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2021-07-23 08:13:17",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
offsetDateTime.format(ISO_OFFSET_DATE_TIME).toString();
然而,第一行给出了错误:
Text ''2021-07-23 08:13:17'' Could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2021-07-23T08:13:17 of type java.time.format.Parsed
如何进行这种简单的转换?
解决方法
您的字符串中没有偏移量,因此它不是 OffsetDateTime
。它只有一个日期和一个时间组件,所以它只是一个 LocalDateTime
。
LocalDateTime localDateTime = OffsetDateTime.parse("2021-07-23 08:13:17",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
之后,您可以使用 atOffset
给它一个偏移量:
OffsetDateTime offsetDateTime = localDateTime.atOffset(ZoneOffset.ofHours(1));
,
您的 String
中没有偏移量,这意味着您必须将其解析为 LocalDateTime
(没有偏移量的 DateTime),然后添加所需的偏移量.
这段代码就是这样做的:
public static void main(String[] args) throws Exception {
String input = "2021-07-23 08:13:17";
// define the formatter for parsing
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss",Locale.ENGLISH);
// then parse to a LocalDateTime and add an offset of +1 hour
OffsetDateTime odt = LocalDateTime.parse(input,dtf)
.atOffset(ZoneOffset.ofHours(1));
System.out.println(odt);
}
输出:
2021-07-23T08:13:17+01:00
c# – 使用Nodatime将UTC时间转换为本地时间
这是我尝试过的:
var pattern = LocalDateTimePattern.CreateWithInvariantCulture("ddMMyyHHmmss"); var parseResult = pattern.Parse(utcDateTime); if (!parseResult.Success) { throw new InvalidDataException("Invalid time specified " + date + time); } var timeZone = DateTimeZoneProviders.Bcl["New Zealand Standard Time"]; var zone = new zoneddatetime( localDateTime,timeZone,timeZone.GetUtcOffset(SystemClock.Instance.Now)); return new DateTime(zone.ToInstant().Ticks);
解决方法
// Since your input value is in UTC,parse it directly as an Instant. var pattern = InstantPattern.CreateWithInvariantCulture("ddMMyyHHmmss"); var parseResult = pattern.Parse("150713192900"); if (!parseResult.Success) throw new InvalidDataException("...whatever..."); var instant = parseResult.Value; Debug.WriteLine(instant); // 2013-07-15T19:29:00Z // You will always be better off with the tzdb,but either of these will work. var timeZone = DateTimeZoneProviders.Tzdb["Pacific/Auckland"]; //var timeZone = DateTimeZoneProviders.Bcl["New Zealand Standard Time"]; // Convert the instant to the zone's local time var zoneddatetime = instant.InZone(timeZone); Debug.WriteLine(zoneddatetime); // Local: 7/16/2013 7:29:00 AM Offset: +12 Zone: Pacific/Auckland // and if you must have a DateTime,get it like this var bclDateTime = zoneddatetime.ToDateTimeUnspecified(); Debug.WriteLine(bclDateTime.ToString("o")); // 2013-07-16T07:29:00.0000000
datetime – 将unix时间戳转换为julian
我发现一个网站(http://aa.usno.navy.mil/data/docs/JulianDate.php)执行类似的计算,但是我需要以编程方式进行.
解决方案可以是C/C++,python,perl,bash等…
所以,在伪代码中:
function float getJulianFromUnix( int unixSecs ) { return ( unixSecs / 86400.0 ) + 2440587.5; }
Java 8将给定的时间和时区转换为UTC时间
我有一个字符串类型就像一个计时:"2015-01-05 17:00"
和ZoneId
是"Australia/Sydney"
。
如何使用Java 8 datetime API将此时间信息转换为与UTC时间相对应的时间?
还需要考虑DST的东西。
答案1
小编典典您正在ZonedDateTime
Java8 中寻找类-带有时区的完整日期时间和与UTC /格林威治标准时间的偏移量。就设计而言,此类应主要视为aLocalDateTime
和a
的组合ZoneId
。的ZoneOffset
是一个至关重要的,但次要的,一条信息,用于确保所述类表示瞬间,特别是在夏令重叠。
例如:
ZoneId australia = ZoneId.of("Australia/Sydney");String str = "2015-01-05 17:00";DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");LocalDateTime localtDateAndTime = LocalDateTime.parse(str, formatter);ZonedDateTime dateAndTimeInSydney = ZonedDateTime.of(localtDateAndTime, australia );System.out.println("Current date and time in a particular timezone : " + dateAndTimeInSydney);ZonedDateTime utcDate = dateAndTimeInSydney.withZoneSameInstant(ZoneOffset.UTC);System.out.println("Current date and time in UTC : " + utcDate);
java – 将时间戳转换为UTC时区
public static long getCurrentEpochTimeStamp(String timeStamp) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.0Z'"); Date date = sdf.parse(timeStamp); return date.getTime(); }
此方法返回epoch当前时间戳,我需要将其转换为UTC时区.
解决方法
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
关于将UTC时间戳转换为Java中的偏移量时间和java unix时间戳转换的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c# – 使用Nodatime将UTC时间转换为本地时间、datetime – 将unix时间戳转换为julian、Java 8将给定的时间和时区转换为UTC时间、java – 将时间戳转换为UTC时区等相关内容,可以在本站寻找。
本文标签: