dateTime Flashcards

1
Q

What package do you import for date and time?

A

import java.time.*;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When do you use LocalDate?
Print the current local Date

A

Your birthday -> just no time
LocalDate.now();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

LocalTime?

A

Contains just time and no date and no timezone.
Hours, minutes, seconds and nanoseconds

LocalTime.now();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

LocalDateTime

A

Contains date and time but no timezone
date and time
LocalDateTime.now();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you use LocalDate and define the method signature?

A

public static LocalDate of(int year, int month, int day)
public static LocalDate of(int year, Month month, int day)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you use LocalTime and define the method signature?

A

LocalTime time1 = LocalTime.of(6,16);

  1. hour and minute
  2. hour, minute and second
  3. hour, minute, second and nanosecond
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can you use LocalDateTime and define the method signature?

A

LocalDateTime time = LocalDateTime.of(date, month,day, hour, month, second, nanos);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you do this?
~~~

LocalDate a = new LocalDate();

~~~

A

NO because the only way you can do is by
LocalDate a = LocalDate.of(2022, 1,3);

It’s static method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you do the following:

LocalDate.of(2022, Month.JANUARY, 40);
A

Throws DateTimeException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Are Date and time classes mutable or immutable?

A

Immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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);

````

A

date.plusDays(2);
date.plusWeeks(1);
date.plusMonths(1);
date.plusYears(1);

date.minusDays(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does this print?

LocalDate date = LocalDate.of(2020, Month.JANUARY,20);
date.plusDays(10);
System.out.println(date);
A

It will pring 20th January 2020 because date and time class are immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the output of the following?
~~~

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date = date.plusMinutes(1);

~~~

A

Does not compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you call the following on LocalDate?

plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos

A

plusYears/minusYears - yes
plusMonths - yes
plusWeeks - yes
plusDays - yes
plusHours - no
plusMinutes -no
plusSeconds - no
plusNanos -no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can you call the following on LocalTime?

plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos

A

plusYears/minusYears - no
plusMonths - no
plusWeeks - no
plusDays - no
plusHours - yes
plusMinutes - yes
plusSeconds - yes
plusNanos - yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you LocalDateTime?
plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos

A

All yes
plusYears/minusYears
plusMonths
plusWeeks
plusDays
plusHours
plusMinutes
plusSeconds
plusNanos

17
Q

Can Period be chainable?

A

No

18
Q

Create a date format?

A

LocalDate date = LocalDate.of(2022, Month.January, 1);
DateTimeFormatter a = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(a.format(date));

19
Q

What is the point of using the Period class?

A

Period class represents a number of days, months, years to add or subtract from a Local Date or LocalDateTime.

20
Q

LocalDate
Calling that on FormatStyle.SHORT
ofLocalizedDate

A

Shows the whole object

21
Q

How to format based on patterns?

A
DateFormatter f = DateFormatter.ofPattern("MMM dd",  yyyy);
22
Q

Make a LocalDateTime and make a dateTimeFormatter

A

LocalDateTime date = LocalDateTime.of(2023, 1, 1, 2,3,3);

	DateTimeFormatter a = DateTimeFormatter.ofPattern("yyyy, MMMM, m, dd");
	System.out.println(a.format(date));
23
Q

Of the DataFormatter patterns,
What does the following mean?
M
MMMM
MM
MMM

A

M means month
M =1;
MMMM = January
MM = 01;
MMM = Jan

24
Q

What does dd represent, hh, yyyyy, mm?

A

d represents day,
yyyy represents year
hh represents hour
: if you want to output the colon
mm = minute

25
Q

LocalDate date = LocalDate.of(2018, Month.APRIL, 40);
System.out.println(date.getYear() + “ “ + date.getMonth() + “ “
+ date.getDayOfMonth());

A

exception is thrown

mistake so know this.

26
Q

LocalDateTime d = LocalDateTime.of(2015, 5, 10, 11, 22, 33);
Period p = Period.ofDays(1).ofYears(2);
d = d.minus(p);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle
.SHORT);
System.out.println(f.format(d));

A

I will be

Year = 2015 -2

The reason is period wont allow chaining so last gets processed.
Mistake.

27
Q

Can you do this?
Period a = Period.ofDays(-14);

A

You can add both negative and positives.

28
Q

What happens when you try to use time with LocalDate

A

DNC