Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet85/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   81   82   83   84   85   86   87   88   ...   245
Bog'liq
Java 17 Recipes

2021-12-06 19:42).
Fri Jan 01 12:00:00 CET 2010
Mon Dec 06 19:42:17 CET 2021
104575 hour(s) 42 min(s)
4357 days
622 weeks
Years between dates: 6
Days between dates:2216
The output of this code is formatted to display strings of text that indicate the 
differences between the current date and the Date object that is created.
Chapter 4 Numbers aNd dates


141
The following is the main class.
public static void main(String[] args){
intervals();
compareDatesCalendar();
}
 How It Works
As with most programmatic techniques, Java has more than one way to perform date 
calculations. The Date-Time API introduced in Java 8 includes new techniques for 
determining time intervals. The Period class determines the difference period between 
two units for specified objects. To obtain a period between two date-time objects, call 
the Period.between() method, passing the two date-time objects for which you’d like 
to obtain the period. The Period class has several methods to break down the intervals 
into different units. For instance, the number of days in the two date-time objects can 
be obtained using the getDays() method. Similarly, the getMonths() and getYears() 
methods can be called to return the number of months or years in the period.
One of the most useful techniques is to perform calculations based on the given 
date’s time in milliseconds. This provides the most accurate calculation because it works 
at a very small interval: milliseconds. The current time in milliseconds can be obtained 
from a Calendar object by calling the getTimeInMillis() method against it. Likewise, 
a Date object returns its value represented in milliseconds by calling the getTime() 
method. As you can see from the solution to this recipe, the first calculation finds the 
difference between the given dates in milliseconds. Obtaining that value and then taking 
its absolute value provides the base needed to perform the date calculations.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   81   82   83   84   85   86   87   88   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling