Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Explain with a code sample how you can check if a date is


Download 1.52 Mb.
Pdf ko'rish
bet89/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   85   86   87   88   89   90   91   92   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain with a code sample how you can check if a date is
before another date.
Answer:
The LocalDate class has a method called isBefore(). You
can use this to check if a date is before another date as shown
below:
LocalDate date1 = LocalDate.of(2018, 7, 24);
LocalDate date2 = LocalDate.parse(“2014–08–11”);
boolean isBefore = date1.isBefore(date2);
System.out.println(“isBefore–”+isBefore);
This code creates two date objects, date1 and date2. It invokes
isBefore() on date1, passing date2 as an argument. Since
date1 is after date2, the isBefore() method returns false.
So, this code prints the output shown below:


235.
236.
isBefore–false
Explain the ZonedDateTime class.
Answer:
The ZonedDateTime class represents a date and time that also
has a time–zone information. So, in addition to the year, month,
day, hour, minutes, seconds and nano seconds components, it
also has a ZoneId component. The ZoneId is an instance of the
java.time.ZoneId and represents the time–zone. There are
about 40 time zones that can be represented via a Zone Id. The
code below shows how you can create a ZonedDateTime:
ZoneId zoneId = ZoneId.of(“Asia/Kolkata”);
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
So, the above code snippet first creates a ZoneId corresponding
to Asia/Calcutta which is the IST time–zone. It then creates a
ZonedDateTime that corresponding to the current time as per
the system clock but with this zone id.
What will be the output of the following code snippet?
LocalTime localTime = LocalTime.of(7, 15);
System.out.println(localTime.minusMinutes(30));
Answer:
The above code creates a LocalTime instance. A LocalTime
represents time that has the hours, minutes, seconds and
nanoseconds component. Here a LocalTime object is created
corresponding to 7–15. The code then invokes the
minusMinutes() method with the value 30 and prints the
output. The minusMinutes() method subtracts the specified



Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   85   86   87   88   89   90   91   92   ...   171




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