Java Tora Razvino Zuva Nguva

/ Ungawana sei iro razvino zuva uye nguva muJava? Muchidzidzo ichi tichatarisa nzira nhatu dzakasiyana mu Java 8.

Iwo makirasi aishandiswa kuratidza zuva uye nguva nde LocalDate, LocalTime, LocalDateTime.

Tora Zuva Razvino

I LocalDate kirasi inoshandiswa kumiririra zuva.

GetCurrentDate.java

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

Kubuda:

2020-02-07

Yakarongwa Zuva

Tinogona kushandisa DateTimeFormatter kirasi yekumisikidza kuratidzwa kwezuva. Semuenzaniso kuratidza iro razvino zuva mune fomati ye yyyy/mm/dd tinoshandisa:



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')));
} }

Kubuda:

2020/02/07

LocalDate kirasi ine dzimwe nzira dzinobatsira dzatinogona kushandisa kuti tiwane ruzivo rwakanyanya nezve zuva razvino senge: 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());
} }

Kubuda:

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

Tora Nguva Yazvino

I LocalTime kirasi inomiririra nguva.

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')));
} }
Ongorora:Tinogona kushandisa DateTimeFormatter yekumisikidza kuratidzwa kwenguva.

Kubuda:

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

LocalTime kirasi ine zvakare anobatsira nzira dzekushandisa kuti uwane rumwe ruzivo nezve iyo iriko nguva:

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());
} }

Kubuda:

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

Tora Razvino Zuva Nguva

Kuti uwane zuva razvino uye nguva, tinogona kushandisa LocalDateTime kirasi

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());
} }

Kubuda:

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