Home > Java > javaTutorial > Introduction to commonly used time APIs and usage methods in Java

Introduction to commonly used time APIs and usage methods in Java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-05-07 09:04:13
forward
1070 people have browsed it

1. The Clock class can be used to access the current date and time. Clock can get the current time zone instead of System.currenttimeMillis().

Clock clock = Clock.systemDefaultZone();long millis = clock.millis();
 
Instant instant = clock.instant();
Date legacyDate = Date.from(instant);   // legacy java.util.Date
Copy after login

2. Time is represented by zoneId, and zoneId can be accessed through the static factory.

System.out.println(ZoneId.getAvailableZoneIds());// prints all available timezone ids
 
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
 
// ZoneRules[currentStandardOffset=+01:00]
// ZoneRules[currentStandardOffset=-03:00]
Copy after login

3. LocalTime means there is no time zone, such as 10pm or 17:30:15.

LocalTime now1 = LocalTime.now(zone1);
LocalTime now2 = LocalTime.now(zone2);
 
System.out.println(now1.isBefore(now2));  // false
 
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
 
System.out.println(hoursBetween);       // -3
System.out.println(minutesBetween);     // -239
Copy after login

The above is the detailed content of Introduction to commonly used time APIs and usage methods in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Laravel adds Api namespace
From 1970-01-01 08:00:00
0
0
0
What does the Laravel api manual do?
From 1970-01-01 08:00:00
0
0
0
javascript - About Baidu Map API calling issues
From 1970-01-01 08:00:00
0
0
0
How to get the Baidu map api
From 1970-01-01 08:00:00
0
0
0
laravel dingo/api installation and configuration
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template