jdk8中的一个极其重要的新特性就是增加了对时间操作的api,这是线程安全的类。所以推荐使用。
- LocalDateTime
这是生成年月日时分秒的是一个类。
- LocalDate
这是生成年月日的类。
- LocalTime
生成时分秒。
这几个类的使用都很方便,但是有一些操作我们需要注意一下。
其中一个是时区问题,如果我们直接使用它们生成时间,他会使用默认的系统时区进行时间生成。
然后我们的mysql使用的时区可能也有对应的问题所以我们在生成时间时应该考虑配置时区:
/**
* Obtains an instance of {@code ZoneId} from an ID ensuring that the
* ID is valid and available for use.
* <p>
* This method parses the ID producing a {@code ZoneId} or {@code ZoneOffset}.
* A {@code ZoneOffset} is returned if the ID is 'Z', or starts with '+' or '-'.
* The result will always be a valid ID for which {@link ZoneRules} can be obtained.
* <p>
* Parsing matches the zone ID step by step as follows.
* <ul>
* <li>If the zone ID equals 'Z', the result is {@code ZoneOffset.UTC}.
* <li>If the zone ID consists of a single letter, the zone ID is invalid
* and {@code DateTimeException} is thrown.
* <li>If the zone ID starts with '+' or '-', the ID is parsed as a
* {@code ZoneOffset} using {@link ZoneOffset#of(String)}.
* <li>If the zone ID equals 'GMT', 'UTC' or 'UT' then the result is a {@code ZoneId}
* with the same ID and rules equivalent to {@code ZoneOffset.UTC}.
* <li>If the zone ID starts with 'UTC+', 'UTC-', 'GMT+', 'GMT-', 'UT+' or 'UT-'
* then the ID is a prefixed offset-based ID. The ID is split in two, with
* a two or three letter prefix and a suffix starting with the sign.
* The suffix is parsed as a {@link ZoneOffset#of(String) ZoneOffset}.
* The result will be a {@code ZoneId} with the specified UTC/GMT/UT prefix
* and the normalized offset ID as per {@link ZoneOffset#getId()}.
* The rules of the returned {@code ZoneId} will be equivalent to the
* parsed {@code ZoneOffset}.
* <li>All other IDs are parsed as region-based zone IDs. Region IDs must
* match the regular expression <code>[A-Za-z][A-Za-z0-9~/._+-]+</code>
* otherwise a {@code DateTimeException} is thrown. If the zone ID is not
* in the configured set of IDs, {@code ZoneRulesException} is thrown.
* The detailed format of the region ID depends on the group supplying the data.
* The default set of data is supplied by the IANA Time Zone Database (TZDB).
* This has region IDs of the form '{area}/{city}', such as 'Europe/Paris' or 'America/New_York'.
* This is compatible with most IDs from {@link java.util.TimeZone}.
* </ul>
*
* @param zoneId the time-zone ID, not null
* @return the zone ID, not null
* @throws DateTimeException if the zone ID has an invalid format
* @throws ZoneRulesException if the zone ID is a region ID that cannot be found
*/
public static ZoneId of(String zoneId) {
return of(zoneId, true);
}
这是对应的部分源码,我们可以传一个字符串来确定时区。
/**
* A map of zone overrides to enable the short time-zone names to be used.
* <p>
* 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.
* <p>
* 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.
* <p>
* This maps as follows:
* <ul>
* <li>EST - -05:00</li>
* <li>HST - -10:00</li>
* <li>MST - -07:00</li>
* <li>ACT - Australia/Darwin</li>
* <li>AET - Australia/Sydney</li>
* <li>AGT - America/Argentina/Buenos_Aires</li>
* <li>ART - Africa/Cairo</li>
* <li>AST - America/Anchorage</li>
* <li>BET - America/Sao_Paulo</li>
* <li>BST - Asia/Dhaka</li>
* <li>CAT - Africa/Harare</li>
* <li>CNT - America/St_Johns</li>
* <li>CST - America/Chicago</li>
* <li>CTT - Asia/Shanghai</li>
* <li>EAT - Africa/Addis_Ababa</li>
* <li>ECT - Europe/Paris</li>
* <li>IET - America/Indiana/Indianapolis</li>
* <li>IST - Asia/Kolkata</li>
* <li>JST - Asia/Tokyo</li>
* <li>MIT - Pacific/Apia</li>
* <li>NET - Asia/Yerevan</li>
* <li>NST - Pacific/Auckland</li>
* <li>PLT - Asia/Karachi</li>
* <li>PNT - America/Phoenix</li>
* <li>PRT - America/Puerto_Rico</li>
* <li>PST - America/Los_Angeles</li>
* <li>SST - Pacific/Guadalcanal</li>
* <li>VST - Asia/Ho_Chi_Minh</li>
* </ul>
* The map is unmodifiable.
*/
这是ZoneId对时区的定义。我们只需传对应的字符串即可。如:LocalDate.now(ZoneId.of("Asia/Shanghai"))。
最后可能会出现mysql远程服务器和本地时间相差13个小时的问题,所以我们应该配置一个数据库的时区
set global time_zone='+08:00';
set time_zone = '+08:00';
至此,解决了两个时区上的问题
扫描二维码关注公众号,回复:
11231620 查看本文章