Giriş
Şu satırı dahil
ederiz.
import java.time.ZonedDateTime*;
Açıklaması
şöyle.
ZonedDateTime is date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.
Yani LocalDateTime + ZoneId + ZoneOffset bilgilerini saklar.
Bu sınıf UTC zaman çizgisindeki bir anı kullanıcıya kendi saat dilimine göre gösterirken
kullanılır. Bu sınıfı UTC zaman çizgisindeki bir an haricinde başka bir şey ile ilklendirmek
yanlıştır. Şeklen
şöyledir.
LocalDateTime Nesnesi ZonedDateTime Nesnesine Çevrilebilir
LocalDateTime Nesnesinden ZonedDateTime Nesnesine Çevrim yazısına taşıdım.
ZonedDateTime Nesnesi İleri Geri Gidilerek Bir Başka Zaman Dilimine Çevrilebilir
Kuala Lumpur'daki yerel saati 7 saat uçuştan sonra Tokyo yerel saatine çevirmek için şöyle
yaparız
ZonedlDateTime klDateTime = ...;
ZonedDateTime japanDateTime = klDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"))
.plusHours(7);
constructor - Instant
Şöyle
yaparız.
Instant effectiveFrom = ...;
ZonedDateTime zdt = effectiveFrom.atZone (ZoneId.systemDefault());
Şöyle de
yapabiliriz.
ZoneId dubai = ZoneId.of("Asia/Dubai");
ZonedDateTime dubaiDT = Instant.now().atZone (dubaiZone);
constructor - LocalDateTime
LocalDateTime Nesnesinden ZonedDateTime Nesnesine Çevrim yazısına taşıdım.
equals metodu
Bu metod yerine isEqual() metodunu kullanmak daha iyi. Elimizde bir nesne
olsun.
ZonedDateTime now = ZonedDateTime.now();
Şöyle
yaparız.
now.withZoneSameInstant(ZoneOffset.UTC)
.equals(now.withZoneSameInstant(ZoneId.of("UTC").normalized()));
from metodu
İmzası
şöyle
public static ZonedDateTime from(TemporalAccessor temporal);
İçi
şöyle
public static ZonedDateTime from(TemporalAccessor temporal) {
if (temporal instanceof ZonedDateTime) {
return (ZonedDateTime) temporal;
}
try {
ZoneId zone = ZoneId.from(temporal);
if (temporal.isSupported(INSTANT_SECONDS)) {
long epochSecond = temporal.getLong(INSTANT_SECONDS);
int nanoOfSecond = temporal.get(NANO_OF_SECOND);
return create(epochSecond, nanoOfSecond, zone);
} else {
LocalDate date = LocalDate.from(temporal);
LocalTime time = LocalTime.from(temporal);
return of(date, time, zone);
}
} catch (DateTimeException ex) {
throw new DateTimeException("...");
}
}
Bu metod her zaman bir ZoneId ile çağrılmalı.Şu kullanım şekli
yanlış.
ZonedDateTime.from(LocalDateTime.parse ("2016-08-31T23:00:59"))
getOffset metoduZoneOffset nesnesi döner
getZone metodu
ZoneID döner. Şöyle
yaparız.
ZonedDateTime.parse("2017-10-27T16:22:27.605-05:30").getZone();
isEqual metodu
Elimizde bir nesne
olsun.
ZonedDateTime now = ZonedDateTime.now();
Şöyle
yaparız.
now.withZoneSameInstant(ZoneOffset.UTC)
.isEqual(now.withZoneSameInstant(ZoneId.of("UTC")));
minus metodu
Şöyle
yaparız.
ZonedDateTime twoWeeksAgo = ZonedDateTime.now(ZoneOffset.UTC).minusWeeks(2);
now metodu
Şöyle yaparız.
ZoneId zoneId_Dubai = ZoneId.of( "Asia/Dubai" );
ZonedDateTime zdt_Dubai = ZonedDateTime.now (zoneId_Dubai);
of metodu - year + month + day + ...
var date = LocalDate.of(2022,Month.JANUARY,1);
var time = LocalTime.of(2,3,4,5);
var datetime = LocalDateTime.of(d, t);
ZonedDateTime.now();
// 2022-10-06T13:34:17.620841485Z[GMT]
var zone = ZoneId.of("US/Pacific");
ZonedDateTime.of(2022, 1, 1, 2, 3, 4, 5, z);
// 2022-01-01T02:03:04.000000005-08:00[US/Pacific]
ZonedDateTime.of(date, time, zone);
//2022-01-01T02:03:04.000000005-08:00[US/Pacific]
ZonedDateTime.of(datetime, zone);
//2022-01-01T02:03:04.000000005-08:00[US/Pacific]
of metodu - LocalDateTime
Şöyle
yaparız.
LocalDateTime ldt = LocalDateTime.of(2015, Month.NOVEMBER, 1, 2, 0);
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("US/Eastern"));
ofInstant metodu
Eğer elimizde UTC zaman noktası varsa şöyle
yaparız.
Instant instant = Instant.now ();
ZoneId zoneId = ZoneId.of ( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant (instant , zoneId );
parse metodu - Zulu
String date = "2021-02-19T00:45:09.798Z";
ZonedDateTime parsedDate = ZonedDateTime.parse(date);
System.out.println(parsedDate);
Şöyle
yaparız.
String s = "2016-08-31T02:04:58.893GMT";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
ZonedDateTime zdtGMT = ZonedDateTime.parse(s, fmt1);
parse metodu - ISO
ISO 8160 string'i parse eder. Şöyle
yaparız.
String s = "2016-04-11T22:56:00.000-0500";
ZonedDateTime zdt = ZonedDateTime.parse(s,DateTimeFormatter.ISO_OFFSET_DATE_TIME);
parse metodu
Verilen string'i parse eder. String saat dilimi bilgisini içerir.
Örnek
Şöyle
yaparız.
String dateTime = "2012-02-22T02:06:58.147Z";
ZonedDateTime d = ZonedDateTime.parse (dateTime);
Örnek
String ISO 8160 ise bayrak vermeye gerek kalmadan şöyle
yaparız.
ZonedDateTime.parse("2017-10-27T16:22:27.605-05:30").getZone();
parse metodu - string + DateTimeFormatter
Verilen string'i belirtilen format'a göre parse eder. Şöyle
yaparız.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("...")
.withLocale(Locale.ENGLISH).withZone(ZoneId.of("Asia/Kolkata"));
ZonedDateTime zdt = ZonedDateTime.parse("...", formatter);
toEpochSecond metodu
Şöyle
yaparız.
ZonedDateTime twoWeeksAgo = ZonedDateTime.now(ZoneOffset.UTC).minusWeeks(2);
long unixTs = twoWeeksAgo.toEpochSecond();
toLocalDate metodu
Başka bir saat dilimindeki zamanı yerel saate çevirir. Şöyle
yaparız.
ZonedDateTime zdt = ...;
LocalDate localDate = zdt.toLocalDate ();
truncatedTo metodu
Şöyle
yaparız.
ZonedDateTime zdt = ...;
zdt.truncatedTo (ChronoUnit.MINUTES));
with metodu
Şöyle
yaparız.
ZonedDateTime.now(ZoneId.of( "Africa/Tunis" ))
.truncatedTo( ChronoUnit.DAYS )
.with(TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ))
.toString()
withZoneSameInstant metodu
GMT saat dilimindeki zamanı Şangay saatine çevirmek için şöyle
yaparız.
ZonedDateTime zdtGMT = ...;
ZonedDateTime zdtChina = zdtGMT.withZoneSameInstant (ZoneId.of("Asia/Shanghai"));