Java 8에서는 ISO 8601 형식의 GMT datetime에서 로컬 datetime (GMT + 1)을 가져 오는 방법이 필요합니다.
간단한 예 : 클라이언트가이 날짜 시간을 나에게 보냅니다 (서버) "2020-01-11T23:00:00.000Z"
. 사용자가 날짜 선택기에서 2020 년 1 월 12 일을 선택할 때 클라이언트 가이 날짜를 보냅니다. GMT + 1의 경우 1 월 12 일이지만 GMT의 경우 전날입니다.
위의 이유로 저는이 날짜 시간이 2020 년 1 월 11 일이 아니라 GMT + 1의 2020 년 1 월 12 일이라는 것을 알고 있습니다.
그래서이 값이 필요합니다 "2020-01-12T00:00:00.000"
정확히 말하면 나는 SimpleDateFormat에 이것을 인쇄 그러나 다만 은밀한 할 필요가 없습니다 "2020-01-11T23:00:00.000Z"
에 "2020-01-12T00:00:00.000"
java.util.Date가 클래스 필드에
감사.
문제는 소스 시스템이 순수한 날짜 값을 취했지만 자정에 시간을 추가 한 다음 UTC로 변환했지만 java.util.Date
기본적으로 로컬 시간대, 즉 JVM의 기본 시간으로 인쇄되는 에서 순수한 날짜 값을 원한다는 것입니다. 존.
따라서 문자열을 구문 분석하고 값을 소스 시스템의 시간대로 되돌려 야하며 현지 시간을 자신의 JVM 기본 시간대의 시간으로 처리해야합니다.
다음과 같이 모든 중간 유형을 표시 할 수 있습니다.
String sourceStr = "2020-01-11T23:00:00.000Z";
ZoneId sourceTimeZone = ZoneOffset.ofHours(1); // Use real zone of source, e.g. ZoneId.of("Europe/Paris");
// Parse Zulu date string as zoned date/time in source time zone
Instant sourceInstant = Instant.parse(sourceStr);
ZonedDateTime sourceZoned = sourceInstant.atZone(sourceTimeZone);
// Convert to util.Date in local time zone
ZonedDateTime localZoned = sourceZoned.withZoneSameLocal(ZoneId.systemDefault());
Instant localInstant = localZoned.toInstant();
Date localDate = Date.from(localInstant); // <== This is your desired result
// Print value in ISO 8601 format
String localStr = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(localDate);
System.out.println(localStr);
산출
2020-01-12T00:00:00.000
물론 코드는 함께 병합 될 수 있습니다.
String input = "2020-01-11T23:00:00.000Z";
Date date = Date.from(Instant.parse(input).atZone(ZoneOffset.ofHours(1))
.withZoneSameLocal(ZoneId.systemDefault()).toInstant());
System.out.println(date);
산출
Sun Jan 12 00:00:00 EST 2020
보시다시피, 내가 미국 동부 시간대에 있더라도 날짜 값은 정확합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다