计算两个不同时区之间的时差?
Time difference calculate between two difference Time Zone?
我想计算两个不同时区之间的时差,例如国家 1 (GMT+05:30) 和国家 2 (GMT+05:00)。如何计算。提前致谢。
您可以使用 java.time.Duration
which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation 找到它。 Java-9 引入了一些更方便的方法。
import java.time.Duration;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(formatDuration(diffBetweenTimeZones("GMT+05:30", "GMT+05:00")));
System.out.println(formatDuration(diffBetweenTimeZones("GMT+05:00", "GMT+05:30")));
// You can use the returned value to get the ZoneOffset which you can use for
// various kinds of processing e.g.
ZoneOffset offset = ZoneOffset.of(formatDuration(diffBetweenTimeZones("GMT+05:30", "GMT+05:00")));
System.out.println(offset);
System.out.println(OffsetDateTime.now(offset));
}
static Duration diffBetweenTimeZones(String tz1, String tz2) {
LocalDate today = LocalDate.now();
return Duration.between(today.atStartOfDay(ZoneId.of(tz1)), today.atStartOfDay(ZoneId.of(tz2)));
}
static String formatDuration(Duration duration) {
long hours = duration.toHours();
long minutes = duration.toMinutes() % 60;
String symbol = hours < 0 || minutes < 0 ? "-" : "+";
return String.format(symbol + "%02d:%02d", Math.abs(hours), Math.abs(minutes));
// ####################################Java-9####################################
// return String.format(symbol + "%02d:%02d", Math.abs(duration.toHoursPart()),
// Math.abs(duration.toMinutesPart()));
// ####################################Java-9####################################
}
}
输出:
+00:30
-00:30
+00:30
2021-03-24T10:37:31.056405+00:30
了解有关现代日期时间的更多信息 API
请注意 java.util
日期时间 API 已过时且容易出错。建议完全停止使用,改用modern date-time API*.
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and 。
我想计算两个不同时区之间的时差,例如国家 1 (GMT+05:30) 和国家 2 (GMT+05:00)。如何计算。提前致谢。
您可以使用 java.time.Duration
which is modelled on ISO-8601 standards and was introduced with Java-8 as part of JSR-310 implementation 找到它。 Java-9 引入了一些更方便的方法。
import java.time.Duration;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(formatDuration(diffBetweenTimeZones("GMT+05:30", "GMT+05:00")));
System.out.println(formatDuration(diffBetweenTimeZones("GMT+05:00", "GMT+05:30")));
// You can use the returned value to get the ZoneOffset which you can use for
// various kinds of processing e.g.
ZoneOffset offset = ZoneOffset.of(formatDuration(diffBetweenTimeZones("GMT+05:30", "GMT+05:00")));
System.out.println(offset);
System.out.println(OffsetDateTime.now(offset));
}
static Duration diffBetweenTimeZones(String tz1, String tz2) {
LocalDate today = LocalDate.now();
return Duration.between(today.atStartOfDay(ZoneId.of(tz1)), today.atStartOfDay(ZoneId.of(tz2)));
}
static String formatDuration(Duration duration) {
long hours = duration.toHours();
long minutes = duration.toMinutes() % 60;
String symbol = hours < 0 || minutes < 0 ? "-" : "+";
return String.format(symbol + "%02d:%02d", Math.abs(hours), Math.abs(minutes));
// ####################################Java-9####################################
// return String.format(symbol + "%02d:%02d", Math.abs(duration.toHoursPart()),
// Math.abs(duration.toMinutesPart()));
// ####################################Java-9####################################
}
}
输出:
+00:30
-00:30
+00:30
2021-03-24T10:37:31.056405+00:30
了解有关现代日期时间的更多信息 API
请注意 java.util
日期时间 API 已过时且容易出错。建议完全停止使用,改用modern date-time API*.
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and