Java Entuk Tanggal Tanggal Saiki

Kepiye carane entuk tanggal lan wektu saiki ing Jawa? Ing tutorial iki, kita bakal nemokake telung cara sing beda ing Java 8.

Kelas sing digunakake kanggo nampilake tanggal lan wektu yaiku LocalDate, LocalTime, LocalDateTime.



Entuk Tanggal Saiki

Sing LocalDate kelas digunakake kanggo makili tanggal.


GetCurrentDate.java

import java.time.LocalDate; public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now.toString());
} }

Output:


2020-02-07

Tanggal Diformat

Kita bisa nggunakake DateTimeFormatter kelas kanggo format tampilan tanggal. Contone kanggo nampilake tanggal saiki ing format yyyy/mm/dd kita nggunakake:



import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now.format(DateTimeFormatter.ofPattern('yyyy/MM/dd')));
} }

Output:

2020/02/07

LocalDate kelas duwe cara migunani liyane sing bisa digunakake kanggo njaluk luwih rinci babagan tanggal saiki kayata: getDayOfWeek(), getDayOfMonth(), getDayOfYear()

import java.time.LocalDate; public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println('Today's date: ' + now.toString());
System.out.println('Day of week: ' + now.getDayOfWeek().toString());
System.out.println('Day of month: ' + now.getDayOfMonth());
System.out.println('Day of year: ' + now.getDayOfYear());
} }

Output:


Today's date: 2020-02-07 Day of week: FRIDAY Day of month: 7 Day of year: 38

Entuk Wektu Saiki

Sing LocalTime kelas makili wektu.

GetCurrentTime.java

import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class GetCurrentTime {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println('Time now: ' + now.toString());
System.out.println('Formatted time: ' + now.format(DateTimeFormatter.ofPattern('HH:mm:ss')));
} }
Cathetan:Kita bisa nggunakake DateTimeFormatter kanggo format tampilan wektu.

Output:

Time now: 00:02:53.313 Formatted time: 00:02:53

LocalTime kelas uga duwe sawetara cara praktis sing migunani kanggo luwih rinci babagan wektu saiki:


import java.time.LocalTime; public class GetCurrentTime {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println('Current hour: ' + now.getHour());
System.out.println('Current minute: ' + now.getMinute());
System.out.println('Current second: ' + now.getSecond());
} }

Output:

Current hour: 0 Current minute: 10 Current second: 16

Entuk Saiki Tanggal Tanggal

Kanggo entuk tanggal saiki lan wektu, kita bisa nggunakake LocalDateTime kelas

package io.devqa.tutorials; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class GetCurrentTime {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now.format(DateTimeFormatter.ofPattern('yyyy/MM/dd - HH:mm:ss')));
System.out.println('Day of month: ' + now.getDayOfMonth());
System.out.println('Current hour: ' + now.getHour());
} }

Output:

2020/02/08 - 00:18:12 Day of month: 8 Current hour: 0