亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

問題

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

解決方式:

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

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

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

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

在這里插入圖片描述

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

在這里插入圖片描述

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

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

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

具體代碼

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

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

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

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

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

相關(guān)文章

最新評(píng)論