TimezoneFmtUtil.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.szwl.util;
  2. import java.time.ZoneId;
  3. import java.time.ZonedDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. public class TimezoneFmtUtil {
  6. public static void main(String[] args) {
  7. // String timeZoneByID = getTimeByZoneID("Asia/Shanghai"); // America/New_York, Asia/Shanghai
  8. // System.out.println(timeZoneByID);
  9. // String nowDateUTC = getNowDateUTC();
  10. // System.out.println("nowDateUTC >>> " + nowDateUTC);
  11. }
  12. /**
  13. * 获取 zoneID 时区的时间
  14. *
  15. */
  16. public static String getTimeByZoneID(String zoneID) {
  17. String formatted = null;
  18. if (ZoneId.getAvailableZoneIds().contains(zoneID)) {
  19. ZoneId zoneId = ZoneId.of(zoneID);
  20. ZonedDateTime now = ZonedDateTime.now(zoneId);
  21. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
  22. formatted = now.format(dateTimeFormatter);
  23. } else {
  24. System.out.println("zoneID有误!");
  25. }
  26. return formatted;
  27. }
  28. /**
  29. * 获取中国时区的UTC格式
  30. *
  31. */
  32. // public static String getNowDateUTC() {
  33. // ZoneId zoneId = ZoneId.of("Asia/Shanghai");
  34. // ZonedDateTime now = ZonedDateTime.now(zoneId);
  35. // DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
  36. // return now.format(dateTimeFormatter);
  37. // }
  38. // public static String getTimeZoneByID(String timeZoneID) {
  39. // // 获取时区对象
  40. // TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
  41. //
  42. // // 设置时区
  43. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  44. // sdf.setTimeZone(timeZone);
  45. //
  46. // // 获取当前时间
  47. // Calendar calendar = Calendar.getInstance();
  48. // calendar.setTimeZone(timeZone);
  49. // Date date = calendar.getTime();
  50. //// String displayName = timeZone.getDisplayName();
  51. //
  52. // // 计算UTC偏移量
  53. // int rawOffset = timeZone.getRawOffset();
  54. // int rawOffsetHours = rawOffset / (1000 * 60 * 60);
  55. //
  56. // int hours = 0;
  57. //// String formatted = null;
  58. // if (ZoneId.getAvailableZoneIds().contains(timeZoneID)) {
  59. // ZoneId zoneId = ZoneId.of(timeZoneID);
  60. //
  61. //// ZonedDateTime now = ZonedDateTime.now(zoneId);
  62. //// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
  63. //// formatted = now.format(dateTimeFormatter);
  64. //
  65. //
  66. // ZoneOffset offset = zoneId.getRules().getOffset(Instant.now()); // 获取当前时刻的偏移量
  67. // int totalSeconds = offset.getTotalSeconds();
  68. // hours = totalSeconds / (60 * 60);
  69. // } else {
  70. // System.out.println("zoneID有误!");
  71. // }
  72. //
  73. // return sdf.format(date) + " UTC " + hours;
  74. //// return formatted;
  75. // }
  76. }