ZonedDateTime etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
ZonedDateTime etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

18 Ağustos 2020 Salı

Instant Nesnesinden ZonedDateTime Nesnesine Çevrim

Giriş
Bir epoch değerine bir başka saat diliminin gözüyle bakabilmeyi sağlar.

Örnek
Şöyle yaparız.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
// Same moment, but viewed through the lens of a region’s wall-clock time.
Örnek
Şöyle yaparız
// A test data
long nextReminder = 1597754387710L;

// Your time-zone e.g. Europe/London
ZoneId zoneId = ZoneId.of("Europe/London");


Instant instant = Instant.ofEpochMilli(nextReminder);
ZonedDateTime zdt= instant.atZone(zoneId);
Örnek
Şöyle yaparız. Bir epoch değeri yani timestamp, sistem saatine çevriliyor. Daha sonra da LocalDateTime nesnesine çevriliyor.
long timeStamp = 1613624926599L;

LocalDateTime dateTime = Instant.ofEpochMilli(timeStamp).atZone(ZoneId.systemDefault())
.toLocalDateTime(); int seconds = dateTime.getSecond();

10 Ağustos 2020 Pazartesi

LocalDateTime Nesnesinden ZonedDateTime Nesnesine Çevrim

Giriş
LocalDateTime nesnesinden ZonedDateTime nesnesine çevrim mümkün. Bunun için atZone() metodu kullanılır

atZone metodu
Örnek - ZoneId Sabiti
Şöyle yaparız
LocalDateTime dateTimeOfLoss = ...;
ZonedDateTime zdt = dateTimeOfLoss.atZone(ZoneId.of(ZoneOffset.UTC.getId()));
Örnek - ZoneId
Şöyle yaparız.
LocalDateTime ldt = ...;
ZoneId     zoneID = ...;
ZonedDateTime zdt = ldt.atZone (zoneId);
Örnek - ZoneId
Şöyle yaparız
LocalDateTime ldt = ...;
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
Örnek - ZoneId
Şöyle yaparız.
LocalDateTime ldt = ...;
ZoneId zoneId = ZoneId.of("Europe/Paris");
ZonedDateTime zdt = ldt.atZone (zoneId);

17 Mart 2020 Salı

java.time API ZonedDateTime Sınıfı - Moment With TimeZone

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 metodu
ZoneOffset 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 + ...
Şöyle yaparız
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
Şöyle yaparız
String date = "2021-02-19T00:45:09.798Z";
ZonedDateTime parsedDate = ZonedDateTime.parse(date);
System.out.println(parsedDate);
Çıktı olarak şunu alırız
2021-02-19T00:45:09.798Z
parse metodu - GMT
Şö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"));