java8时间api和mysql集成的坑

java8时间api和mysql集成的坑

代码

Timestamp.valueOf(LocalDateTime.now) 

现象是时间相差14个小时,想了下,相差14个小时应该是芝加哥时间

跟踪jdk源码,ZoneId


    /**
     * A map of zone overrides to enable the short time-zone names to be used.
     * 

* Use of short zone IDs has been deprecated in {@code java.util.TimeZone}. * This map allows the IDs to continue to be used via the * {@link #of(String, Map)} factory method. *

* This map contains a mapping of the IDs that is in line with TZDB 2005r and * later, where 'EST', 'MST' and 'HST' map to IDs which do not include daylight * savings. *

* This maps as follows: *

    *
  • EST - -05:00
  • *
  • HST - -10:00
  • *
  • MST - -07:00
  • *
  • ACT - Australia/Darwin
  • *
  • AET - Australia/Sydney
  • *
  • AGT - America/Argentina/Buenos_Aires
  • *
  • ART - Africa/Cairo
  • *
  • AST - America/Anchorage
  • *
  • BET - America/Sao_Paulo
  • *
  • BST - Asia/Dhaka
  • *
  • CAT - Africa/Harare
  • *
  • CNT - America/St_Johns
  • *
  • CST - America/Chicago
  • *
  • CTT - Asia/Shanghai
  • *
  • EAT - Africa/Addis_Ababa
  • *
  • ECT - Europe/Paris
  • *
  • IET - America/Indiana/Indianapolis
  • *
  • IST - Asia/Kolkata
  • *
  • JST - Asia/Tokyo
  • *
  • MIT - Pacific/Apia
  • *
  • NET - Asia/Yerevan
  • *
  • NST - Pacific/Auckland
  • *
  • PLT - Asia/Karachi
  • *
  • PNT - America/Phoenix
  • *
  • PRT - America/Puerto_Rico
  • *
  • PST - America/Los_Angeles
  • *
  • SST - Pacific/Guadalcanal
  • *
  • VST - Asia/Ho_Chi_Minh
  • *
* The map is unmodifiable. */
public static final Map SHORT_IDS;

结果CST是芝加哥时间,然后去mysql中看了一下

mysql> show variables like "%time_zone%";
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | CST    |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 20:14:19  |
+-----------+
1 row in set (0.00 sec)

坑了吧,这里CST是China Standard Time,最后用了SimpleDateFormat加ThreadLocal搞定的

你可能感兴趣的:(scala)