Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet77/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   73   74   75   76   77   78   79   80   ...   245
Bog'liq
Java 17 Recipes

 Solution 1
Use the LocalDateTime class, which is part of the new Date-Time API, to capture and 
display the current date and time. The LocalDateTime class contains a method named 
now(), which obtains the current date and time. The following lines of code demonstrate 
how to do so.
LocalDateTime ldt = LocalDateTime.now();
System.out.println("Local Date and Time: " + ldt);
The resulting LocalDateTime object contains both the date and time but no time zone 
information. The LocalDateTime class also contains additional methods that provide 
options for working with date-time data. For instance, to return a LocalDateTime object 
with a specified date and time, pass parameters of int type to the LocalDateTime.of() 
method, as follows.
// Obtain the LocalDateTime object of the date 11/11/2021 at 12:00
LocalDateTime ldt2 = LocalDateTime.of(2021, Month.NOVEMBER, 11, 12, 00);
The following examples demonstrate a handful of the methods that are available in a 
LocalDateTime object.
public static void obtainDatesWithTime(){
LocalDateTime ldt = LocalDateTime.now();
System.out.println("Local Date and Time: " + ldt);
// Obtain the LocalDateTime object of the date 11/11/2021 at 12:00
LocalDateTime ldt2 = LocalDateTime.of(2021, Month.NOVEMBER, 11, 
12, 00);
System.out.println("Specified Date and Time: " + ldt2);
// Obtain the month from LocalDateTime object
Chapter 4 Numbers aNd dates


127
Month month = ldt.getMonth();
int monthValue = ldt.getMonthValue();
System.out.println("Month: " + month);
System.out.println("Month Value: " + monthValue);
// Obtain day of Month, Week, and Year
int day = ldt.getDayOfMonth();
DayOfWeek dayWeek = ldt.getDayOfWeek();
int dayOfYr = ldt.getDayOfYear();
System.out.println("Day: " + day);
System.out.println("Day Of Week: " + dayWeek);
System.out.println("Day of Year: " + dayOfYr);
// Obtain year
int year = ldt.getYear();
System.out.println("Date: " + monthValue + "/" + day + "/" + year);
int hour = ldt.getHour();
int minute = ldt.getMinute();
int second = ldt.getSecond();
System.out.println("Current Time: " + hour + ":" + minute +
":" + second);
// Calculation of Months, etc.
LocalDateTime currMinusMonths = ldt.minusMonths(12);
LocalDateTime currMinusHours = ldt.minusHours(10);
LocalDateTime currPlusDays = ldt.plusDays(30);
System.out.println("Current Date and Time Minus 12 Months:
" + currMinusMonths);
System.out.println("Current Date and Time MInus 10 Hours:
" + currMinusHours);
System.out.println("Current Date and Time Plus 30 Days:"
+ currPlusDays);
}
Chapter 4 Numbers aNd dates


128
Here’s the result.
Local Date and Time: 2021-12-06T19:37:53.422560400
Specified Date and Time: 2021-11-11T12:00
Month: DECEMBER
Month Value: 12
Day: 6
Day Of Week: MONDAY
Day of Year: 340
Date: 12/6/2021
Current Time: 19:37:53
Current Date and Time Minus 12 Months: 2020-12-06T19:37:53.422560400
Current Date and Time MInus 10 Hours: 2021-12-06T09:37:53.422560400
Current Date and Time Plus 30 Days:2022-01-05T19:37:53.422560400
The main is:
public static void main(String[] args){
obtainDatesWithTime();
}

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   73   74   75   76   77   78   79   80   ...   245




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