Java Dates and Times Flashcards

1
Q

which package to import to work with date and time classes?

A

java.time.*

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

what are the four choices of Date and time usage?

A

LocalDate, LocalTime, LocalDateTime, ZonedDateTime

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

LocalDate

A

No Time, no timezone. Just Date. Eg.., My birthday

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

LocalTime

A

No date, no timezone. Just time. Eg..Midnight

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

LocalDateTime

A

Contains both date and time, no time zone. Eg.., Stroke of midnight on new years eve.

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

ZonedDateTime

A

contains date, time, timezone. Eg.., conference call at 9 a.m. EST

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

Example of different time zone offsets that can be listed

A

+02.00 or GMT+2 or UTC+2

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

Different ways to pass the month value for a date

A

Either MONTH ENUM(eg.. Month.JANUARY) or pass an int number.

The int starts with 1. Eg.. 1 for January

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

Month month = Month.JANUARY;
boolean b1=month==1;
boolean b2= month==Month.APRIL;
Output?

A

b1 does not compile. MONTH is an enum and cannot be compared with int.
b2 returns false as month is JANUARY

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

What is the first thing to do to get the ZonedDateTime?

A

We need to get the desired time zone.

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

How to create a zoned date time?

A

ZoneId zone = ZoneId.of(“US/Eastern”);

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

Three approaches to get the ZonedDateTime

A
  1. Pass all the values individually.
    ZonedDateTime zoneTime = ZonedDateTime.of(2017, 8, 6, 11, 27, 30,200, zone);
  2. Pass the LocalDate, LocaltTme and Zone objects
    ZonedDateTime zoneTime = ZonedDateTime.of(LocalDateObject, LocalTimeObject, zone);
  3. Pass the LocalDateTime and zone object.
    ZonedDateTime.of(LocalDateTimeObject, zone);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

One important thing to consider for ZonedDateTime object Creation

A

There is no option to pass in the Month Enum

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

To find the System time zone

A

ZoneId.systemDefault()

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

Why we cant invoke a constructor for Date and Time classes

A

Dates and Time classes have private constructors. We cannot invoke them. We have to use their static methods for any operations.

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

LocalDate date = new LocalDate(); What will happen

A

It will not compile

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

LocalDate date = LocalDate.of(2017, Month.JANUARY, 32);

A

It throws DateTimeException as it has invalid dates

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

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

date = date.plusMinutes(1);

A

It does not compile.

We are trying to add the time to the date object.

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

What are the different approaches to create a period Object?

A
Period p1 = Period.of(years, months, days);
Period p2 = Period.ofDays(1);
Period p3 = Period.ofWeeks(weeks);
Period p4 = Period.ofMonths(months);
Period p5 = Period.ofYears(years);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is one important catch about Period?

A

You cannot chain the period methods. If you do it returns the last period operation value with a compile warning, remember that Period is immutable.

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

If we print the period what is the format it is printed?

Eg,,. Period.of(1, 2, 3)

A

P1Y2M3D
P -> To show that it is a period measure
1Y -> No of Years with Y indicating the year
2M-> No of Months with M indicating the month
3D-> No of days with D indicating the Days

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

An important point to remember on displaying the Period?

A

If any of the values (either Year or month or days) are not available, then they are omitted

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

Period.ofMonths(3)

A

P3M

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

Period.of(0,20,47)

A

P20M47D. No issues in Period with Days greater than the no of days in month OR MONTH GREATER THAN THE NO OF MONTHS IN A YEAR

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

Period.ofWeeks(3)

A

P21D :)

Weeks are not one of the units a Period stores. So, it converts into a number of days and stores them.

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

What happens when we try to use a Period of Month to LocalTime?

A

UnsupportedTemporalTypeexception

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

What is Duration? why we use duration when we have Period?

A
  1. Duration is intended for smaller units of time.

2. Period is for Days and more of a time

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

How does the Period and Duration Outputs starts with?

A

Period: P
Duration: PT -> Period of Time

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

What are all the units that can be specified for Duration?

A

Days, hours, minutes, Seconds, milliseconds, nanoseconds

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

Different ways of creating the Duration

A
Duration.ofDays(1);
Duration.ofHours(1);
Duration.ofMinutes(1);
Duration.ofSeconds(1);
Duration.ofMillis(1);
Duration.ofNanos(1);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What does duration doesnt have on comparing to Period?

A

It doesnt have the constructor that takes three units

32
Q

What is another factory method that Duration takes?

A

A method with number and TemporalUnit

33
Q

What is temporal Unit

A

An interface

34
Q

What is the implementation of TemporalUnit?

A

ChronoUnit

35
Q

Where can a Duration and Period can be used?

A

Period:

  1. LocalDate
  2. LocalDateTime
  3. ZonedDateTime

Duration:

  1. LocalTime
  2. LocalDateTime
  3. ZonedDateTime
36
Q

What is Instant Class

A

Instant class represents a specific moment in the GMT time zone

37
Q

How to get the current time using Instant

A

Instant instant = Instant.now();

38
Q

Can we convert a LocalDateTime to Instant? Why?

A

we cannot convert a localDateTime to instant because, LocalDateTime does not contain a Timezone and Instant contain a time zone in it.

39
Q

What to keep in mind while doing math Operation on Instant Class

A

Instant class allows doing Math operation with Days or units lesser than That. If you try to add a week, month or year we get Exceptions

40
Q

What is the method for add operation in Instant?

A

plus();

Eg../ Instant.plus(1, Chronounit.DAYS or Hours or any lesser)

41
Q

How does oracle defines Locale?

A

A specific geographical or political or cultural region

42
Q

Locale class is in which package

A

java.util;

43
Q

How will you get the Locale of your computer?

A

Locale locale = Locale.getDefault();

44
Q

Format for Locale. Which are optional?

A

two lowercase language code followed by ‘_’ and two uppercase country code letters.

_ and Country codes are optional. You can have only Language code as Locale information.

45
Q

Three ways of creating a Locale?

A
  1. Locale class provides the constants for some of the most commonly used Locales
  2. Use constructors with Just passing a language
    new Locale(“fr”);
  3. Use constructors by passing a language and country code.
    new Locale(“hi”, “IN”);
46
Q

Will java allow you to create Locale with invalid language or country code

A

Yes. But your program will behave wierdly.

47
Q

What is the other way to create a Locale apart from the other 3

A

Using Builder design pattern. It lets you set all the properties that we want to set and let us Build

48
Q

Eg of using Builder design pattern to create a locale effectively

A

Locale l1 = new Locale.Builder.
setRegion(“US”).
setLanguage(“en”).
build();

49
Q

How not to use Locale.Builder?

A

setting country code with lowercase or language code with Upper case or creating an empty Locale. It causes confusion.

50
Q

How to change the Locale of the system. Eg..?

A
Locale locale = new Locale("de", "DE");
locale.setDefault(locale);
51
Q

What does Resource Bundles contain?

A

Resource bundles contains the locale specific object to be used by the program. It has map, key vales pair

52
Q

Which method allows for a default value?

getDefault or get?in Property class

A

getProperty()

53
Q

Which class to extend on creating a Java class resource bundle? Explain that class and methods

A

ListResourceBundle. It is a abstract class leaves one method for subclasses to implement. The method is getContents.

54
Q

What are the two advantages of having a Java class Resource bundles? and one limitations?

A
  1. You can use value type that is not a string
  2. We can create values for Properties are runtime.

Limits:
1. The key type should be a string though

55
Q

what are the two methods for creating a resource bundle?

A

ResourceBundle.getBundle(“name”);

ResourceBundle.getBundle(“name”, locale);

56
Q

Picking a resource Bundle steps

A
  1. Always look for the property files after the matching java class
  2. Drop one thing at a time if there are no matches. First drop country code and then the language code
  3. Look at the default Locale at the end
57
Q

what is the trick for finding a particular value with a key

A

Java first looks for the key in the most specifi locale.java class and then Locale.properties and then its parent. So It can read values from its parent too.

58
Q

which package has the class for Number Formatting?

A

java.text.*;

59
Q

What is format and parse?

A

format() -> convert a number into String

parse() -> Convert a string into a number

60
Q

Do format classes are thread safe?

A

No, they are not thread safe. Should not save in instance variable or static variables.

61
Q

What is the exception that parse method throws?

A

It throws a checkd Exception ParseException

62
Q

What does parse method does with extra characters while parsing?

A

The parser method parses only the beginning of the String.If the parser method reaches a character that dos not be parsed, it stopes there and returns the parsed value

63
Q
NumbarFormat nf = NumberFormat.getInstance();
String one = "456vdgg"
String two = "-2.156x10"
String three = "xfgfg776"
nf.parse(one);
nf.parse(two);
nf.parse(three);
Output?
A

one = 456;
two = -2.156
three =ParseException because there are no numbers in the beginning of the String.

64
Q

What is the return value of the Parse method?

A
Number object.
Number is a parent class of Java.lang wrapper class so it can be casted
65
Q

What is the class used for Formatting Dates? Which package? Which method?

A

DateTimeFormatter.

java.time.format pacakage.

It can format any type of date and/or time Object.

format() method

66
Q

Thera res some predefined DateTimeFormatter. Eg?

A

DateTimeFormatter shortDateTiime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

67
Q

What is so special about format() method?

A

The format method can be declared on both Formatter object and date/time objects.

68
Q

What happens when we try to format time using DateFormatted?

A

UnsupportedTemporalTypeException.

69
Q

What should we do if we want a new Formatter?

A

DateTimeFormatter df = DateTimeFormatter.ofPattern(“MMMM dd, yyy, hh:mm”);

70
Q

In using Property class, which overloaded method to use so that if the value of a property is not set, default value that we pass is returned?

A

Property;s getProperty(key, “default value to be returned if not present”);

71
Q

What is the limitation of using property file as resource bundle?

A

It can have the property value only as String.

Note that key will always be String using property file or class file

72
Q

Example for Using Java resource bundles

A

import java.util.*;

2: public class Zoo_en extends ListResourceBundle {
3: protected Object[][] getContents() {
4: return new Object[][] {
5: { “hello”, “Hello” },
6: { “open”, “The zoo is open” } };
7: } }

73
Q

Different NumberFormat Instances?

A
NumberFormat.getInstance()
NumberFormat.getInstance(locale)
NumberFormat.getNumberInstance()
NumberFormat.getNumberInstance(locale)
================For formatting monetary
amounts======================
NumberFormat.getCurrencyInstance()
NumberFormat.getCurrencyInstance(locale)
===================For formatting percentages ==========================NumberFormat.getPercentInstance()
NumberFormat.getPercentInstance(locale)
======================Rounds decimal values before
displaying (not on the exam)========================
NumberFormat.getIntegerInstance()
NumberFormat.getIntegerInstance(locale)
74
Q

How to get all the availableZoneIds?

A

ZoneId.getAvaliableZoneId()

75
Q

How to get next Thursday date from Today?

A

LocalDateTime date = LocalDateTime.now().with(TemporalAdjusters.next(DayOfWeek.THURSDAY));

76
Q

What is Period or Duration and Instants

A

Period- Specification of time in Day, week and month
Duration: Minustes, hours
Instants: Instance of time