Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet74/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   70   71   72   73   74   75   76   77   ...   245
Bog'liq
Java 17 Recipes

 Solution 2
To obtain the month-day for the current date or a specified date, simply use the java.
time.MonthDay class. The following lines of code demonstrate how to obtain a month- 
day combination.
public static void obtainMonthDay(){
MonthDay monthDay = MonthDay.now();
System.out.println("Current month and day: " + monthDay);
MonthDay specifiedDate = MonthDay.of(Month.NOVEMBER, 12);
System.out.println("Specified Month-Day: " + specifiedDate);
}
Here’s the result.
Current month and day: --10-24
Specified Month-Day: --11-12
The main is:
public static void main(String[] args) {
obtainYearMonth();
obtainMonthDay();
}
Note that by default, MonthDay does not return a very useful format.
Chapter 4 Numbers aNd dates


123
 How It Works
The Date-Time API includes classes that make it easy to obtain the date information 
that your application requires. Two of those are the YearMonth and MonthDay classes. 
The YearMonth class obtains the date in year-month format. It contains a few methods 
that obtain the year-month combination. As demonstrated in the solution, you can call 
the now() method to obtain the current year-month combination. Like the LocalDate 
class, YearMonth also contains an of() method that accepts a year in int format and a 
number that represents the month of the year. In the solution, Month enum obtains the 
month value.
Like the YearMonth class, MonthDay obtains the date in a month-day format. It also 
contains a few different methods for obtaining the month-day combination. Solution 2
demonstrates two such techniques. Obtaining the current month-day combination 
by calling the now() method and using the of() method to obtain a month-day 
combination for a specified date. The of() method accepts an int value for the month 
of the year as its first parameter, and for the second parameter it accepts an int value 
indicating the day of the month.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   70   71   72   73   74   75   76   77   ...   245




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