dateTime Flashcards
What package do you import for date and time?
import java.time.*;
When do you use LocalDate?
Print the current local Date
Your birthday -> just no time
LocalDate.now();
LocalTime?
Contains just time and no date and no timezone.
Hours, minutes, seconds and nanoseconds
LocalTime.now();
LocalDateTime
Contains date and time but no timezone
date and time
LocalDateTime.now();
Can you use LocalDate and define the method signature?
public static LocalDate of(int year, int month, int day)
public static LocalDate of(int year, Month month, int day)
Can you use LocalTime and define the method signature?
LocalTime time1 = LocalTime.of(6,16);
- hour and minute
- hour, minute and second
- hour, minute, second and nanosecond
Can you use LocalDateTime and define the method signature?
LocalDateTime time = LocalDateTime.of(date, month,day, hour, month, second, nanos);
Can you do this?
~~~
LocalDate a = new LocalDate();
~~~
NO because the only way you can do is by
LocalDate a = LocalDate.of(2022, 1,3);
It’s static method.
Can you do the following:
LocalDate.of(2022, Month.JANUARY, 40);
Throws DateTimeException
Are Date and time classes mutable or immutable?
Immutable
Given the following:
Can you add weeks, days, months and years?
Can you subtract them as well?
~~~
LocalDate date = LocalDate.of(2022, Month.JANUARY, 20);
````
date.plusDays(2);
date.plusWeeks(1);
date.plusMonths(1);
date.plusYears(1);
date.minusDays(2);
What does this print?
LocalDate date = LocalDate.of(2020, Month.JANUARY,20); date.plusDays(10); System.out.println(date);
It will pring 20th January 2020 because date and time class are immutable
What is the output of the following?
~~~
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date = date.plusMinutes(1);
~~~
Does not compile
Can you call the following on LocalDate?
plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos
plusYears/minusYears - yes
plusMonths - yes
plusWeeks - yes
plusDays - yes
plusHours - no
plusMinutes -no
plusSeconds - no
plusNanos -no
Can you call the following on LocalTime?
plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos
plusYears/minusYears - no
plusMonths - no
plusWeeks - no
plusDays - no
plusHours - yes
plusMinutes - yes
plusSeconds - yes
plusNanos - yes