欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jvm中指定時區(qū)信息user.timezone問題及解決方式

 更新時間:2023年02月20日 09:05:31   作者:russle  
同一份程序使用時間LocalDateTime類型,在國內(nèi)和國外部署后,返回的時間信息前端使用出問題,這篇文章主要介紹了jvm中指定時區(qū)信息user.timezone問題及解決方法,需要的朋友可以參考下

問題

同一份程序使用時間LocalDateTime類型,在國內(nèi)和國外部署后,返回的時間信息前端使用出問題。 因為LocalDateTime不帶時區(qū)信息,國內(nèi)調(diào)用后,前端頁面默認(rèn)使用的瀏覽器所在os的時區(qū)(我們的系統(tǒng)中沒有給用戶設(shè)置時區(qū)), 因此會出現(xiàn)時間不一致, 或者判斷超時了,但是實(shí)際上沒有超時的問題。

解決方式:

要么返回timestamp數(shù)字類型,前端自己解析。 缺點(diǎn):直接使用api的同事不方便看操作時間信息。

用戶可以可以在個的profile中設(shè)置時區(qū),方便各個時區(qū)用戶在一個系統(tǒng)中操作。 缺點(diǎn):改動較多。

最后的折中方法:
后端內(nèi)部使用ZonedDateTime,返回的時間中帶上時區(qū)信息。 備注:這里應(yīng)用系統(tǒng)沒有使用數(shù)據(jù)庫,因為沒有使用數(shù)據(jù)庫時間格式。

這里??遇到一個問題,國內(nèi)機(jī)器都是時區(qū)為

在這里插入圖片描述

springboot 程序啟動后,ZoneDateTime 格式默認(rèn)是"2023-02-16T21:44:31.914407+08:00";

在這里插入圖片描述

但是國外的機(jī)器不行,依然不帶時區(qū)信息。

在jvm啟動參數(shù)中指定時區(qū)信息
國內(nèi)啟動不指定時間,os默認(rèn)的是"Asia/Shanghai"。 國外的啟動參數(shù)指定為-Duser.timezone=CET

示意:(這里是示意,省略其他參數(shù),實(shí)際參數(shù)要跟多)
Java -jar -Dspring.profiles.active=dev -Duser.timezone=CET app.jar

具體代碼

1,ObjectMapper中設(shè)置時區(qū)和時間格式

    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();

    mapper.setTimeZone(TimeZone.getDefault()); // 在本項目必須有這樣,某則有些接口中返回的ZonedDateTime序列化后不帶時區(qū)信息,添加這行就會帶上時區(qū)信息
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return mapper;

2, 其他區(qū)地方解析返回的時間
引入依賴包

	<dependency>
		<groupId>joda-time</groupId>
		<artifactId>joda-time</artifactId>
		<version>2.12.2</version>
	</dependency>
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

   DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ").withZoneUTC();

    //  String str = "2022-02-16T21:44:31.914407+08:00";
    String str = "2022-02-17T14:35:48.8932+08:00";

    //String str = "2022-02-16T21:44:31+09:00";
    DateTime dateTime = formatter.parseDateTime(str);
    log.info("dateTime:{}", dateTime);
    String strAgain = dateTime.toString(formatter);
    log.info("strAgain:{}", strAgain);

到此這篇關(guān)于jvm中指定時區(qū)信息user.timezone的文章就介紹到這了,更多相關(guān)jvm時區(qū)信息user.timezone內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論