Giriş
Bu sınıf tarih ve saati bilir. Diğer sınıflar ile ilişkisi
şöyledir. Bu sınıf sistem saatinin çözünürlüğü kadar çözünürlük sağlar.
LocalDateTime Zaman Çizgisi ve TimeZone Bilmez
Zaman çizgisi üzerindeki bir noktayı temsil etmez. Kısaca
no offset,
no zone,
not a moment denilebilir. Açıklaması
şöyle.
This class does not store or represent a time-zone. Instead, it is a description of the date, as used for birthdays, combined with the local time as seen on a wall clock. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
Şeklen
şöyle.
O Zaman LocalDateTime Nedir ?
Açıklaması
şöyle. Yani sadece bir string gibi düşünmek belki anlamayı daha kolaylaştırır.
LocalDateTime is date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30
LocalDateTime Immutable'dır
Açıklaması
şöyle. Aslında bunda şaşıracak bir şey yok. Java'daki bir çok sınıf artık Immutable
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.
LocalDateTime Moment Bilmiyorsa, Moment With TimeZone İçin Ne Kullanırız?
Eğer amacımız hem moment hem de time zone bilgisini saklamak ise
ZonedDateTime sınıfını kullanmak
gerekir.
LocalDateTime Nesnesini String'e Çevirme1. format() metodu türevleri kullanılabilir. format metodu içine DateTimeFormatter alır.
2. toString() tarih ve zaman değerleri arasında T harfini koyduğu için pek kullanışlı değil. Çıktı olarak şuna
benzer
2020-04-01T11:54:05.514
toString en kısa çıktıyi verir. Yani eğer saniye 00 ise LocalDateTime Bunu çıktı olarak vermez. Açıklaması
şöyle
The output will be one of the following ISO-8601 formats:
uuuu-MM-dd'T'HH:mm
uuuu-MM-dd'T'HH:mm:ss
uuuu-MM-dd'T'HH:mm:ss.SSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSS
uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
Örnek
Elimizde şöyle bir kod
olsunList <LocalDateTime> times = Arrays.asList(
LocalDateTime.now(),
LocalDateTime.parse("2020-09-13T20:53", DateTimeFormatter.ISO_LOCAL_DATE_TIME),
LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
);
for (LocalDateTime time: times)
System.out.println("Time: " + time);
Time: 2020-09-13T18:42:25.775
Time: 2020-09-13T20:53
Time: 2020-09-13T18:42:25.779
Bunun çözümü kendi kodladığımız bir DateTimeFormatter kullanmak. Şöyle
yaparız
public static void main(String[] args) {
DateTimeFormatter isoDtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
List <LocalDateTime> times =
Arrays.asList(LocalDateTime.now(),
LocalDateTime.parse("2020-09-13T20:53", isoDtf),
LocalDateTime.parse(LocalDateTime.now().format(isoDtf)));
DateTimeFormatter outputFormatter = DateTimeFormatter
.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
for (LocalDateTime time: times)
System.out.println("Time: "+ time.format(outputFormatter));
}
Time: 2020-09-15T15:09:08.011
Time: 2020-09-13T20:53:00.000
Time: 2020-09-15T15:09:08.027
atZone metodu
LocalDateTime Nesnesinden ZonedDateTime Nesnesine Çevrim yazısına taşıdım.
format metodu - Hazır DateTimeFormatter Şablonu
Örnek
Şöyle
yaparız.
System.out.println(ldt.format(DateTimeFormatter.ISO_TIME));
format metodu - Kendi DateTimeFormatter Şablonumuz
Örnek
Şöyle
yaparız.
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS");
System.out.println(ldt.format(formatter));
Çıktı olarak şunu
alırız.
2016-08-12T09:38:12.123456789
getDayOfMonth metodu
Şöyle
yaparız.
ldt.
getDayOfMonth();
getHour metodu
Şöyle
yaparız.
ldt.
getHour();
getMinute metodu
Şöyle
yaparız.
ldt.
getMinute();
getMonthValue metodu
Şöyle
yaparız.
ldt.getMonthValue
();
getSecond metodu
Şöyle
yaparız.
ldt.getSecond
();
getYear metodu
Şöyle
yaparız.
ldt.getYear();
minusDays metodu
Şöyle
yaparız.
LocalDateTime ldt = LocalDateTime.now().minusDays(30);
now metodu
Sistemin varsayılan Clock sınıfını
kullanır. Şöyle yaparız.
LocalDateTime ldt = LocalDateTime.now();
now metodu - clock
Şöyle
yaparız.
Clock clock = ...;
LocalDateTime ldt =
LocalDateTime.now(clock); // Instead of just LocalDateTime.now()
of metodu - LocalDate + LocalTime
Şöyle
yaparız.
LocalDate ld = LocalDate.of( 2018 , Month.DECEMBER , 25 ) ;
LocalTime lt = LocalTime.MIN ; // 00:00:00
LocalDateTime ldt = LocalDateTime.of( ld , lt ) ; // Xmas morning anywhere.
of metodu - year + month + dayOfMonth - hour + minute + second + nanosecond
Şöyle
yaparız.
LocalDateTime when =
LocalDateTime.of(2016, Month.AUGUST, 12, 9, 38, 12, 123456789);
Eğer yazdırırsak nanosaniye çözünürlüğünde veri saklayabildiğimiz görürüz. Şöyle
yaparız.
System.out.println(when);
Çıktı olarak şunu
alırız.
2016-08-12T09:38:12.123456789
Örnek
LocalDateTime.now();
// 2022-10-06T13:34:17.61565480
LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5));
// 2022-01-01T02:03:04.000000005
var d = LocalDate.of(2022,Month.JANUARY,1);
var t = LocalTime.of(2,3,4,5);
var dt = LocalDateTime.of(d, t);
// 2022-01-01T02:03:04.000000005
ofEpochSeconds metodu
Şöyle
yaparız.
int seconds = ...;
int nanoSeconds = ...;
LocalDateTime t = LocalDateTime.ofEpochSecond(seconds, nanoSeconds, ZoneOffset.UTC);
ofInstant metodu - Instant
Belirtilen Instant'ı yani UTC zaman noktasını belirtilen saat dilimine göre uyarlayarak o dilimin yerel saatine çevirir.
Örnek
Şöyle
yaparız.
Calendar cal = Calendar.getInstance();
LocalDateTime ldt = LocalDateTime.ofInstant (cal.toInstant(),
ZoneId.systemDefault());
Örnek
Şöyle
yaparız.
Date date = ...;
long timestamp = date.getTime();
LocalDateTime ldt = LocalDateTime.ofInstant (Instant.ofEpochMilli(timestamp),
ZoneId.systemDefault());
parse metodu
Şöyle
yaparız.
LocalDateTime ldt = LocalDateTime.parse ("2016-04-04T08:00" );
truncatedTo metodu
Farklı makinelerde farklı çözünürlük elde edilebilir.
Örnek
Çözünürlüğü değiştirerek nanosaniyeden milisaniye seviyesine getirmek için şöyle
yaparız.
LocalDateTime.now(ZoneId.of("Europe/Berlin")).truncatedTo(ChronoUnit.MILLIS);
Örnek
Şöyle
yaparız. Burada saat x:02 ise saat başına truncate ediliyor.
String[] arr = { "2021-02-08T19:02:49.594", "2021-02-08T19:56:49.594",
"2021-02-08T19:54:49.594",
"2021-02-08T19:06:49.594" };
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS");
for (String s : arr) {
System.out.println(roundToNearestHour(s).format(dtf));
}
static LocalDateTime roundToNearestHour(String str) {
LocalDateTime ldt = LocalDateTime.parse(str);
int minute = ldt.getMinute();
return minute < 5 ? ldt.truncatedTo(ChronoUnit.HOURS)
: (minute >= 55 ? ldt.plusHours(1).truncatedTo(ChronoUnit.HOURS) : ldt);
}
2021-02-08T19:00:00.000
2021-02-08T20:00:00.000
2021-02-08T19:54:49.594
2021-02-08T19:06:49.594
toString metodu
Açıklaması
şöyle.
The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.
Çıktı olarak şunu
alırız. Sistem saati genellikle milisaniye çözünürlüğünde olduğu için çıktı da bu çözünürlüktedir.
2016-08-11T22:17:35.031