Dates, Strings, and Localization Flashcards

1
Q

Package where most date and time classes are

A

java.time

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

LocalDate

A

Contains just a date—no time and no time zone.

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

LocalTime

A

Contains just a time—no date and no time zone.

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

LocalDateTime

A

Contains both a date and time but no time zone.

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

ZonedDateTime

A

Contains a date, time, and time zone.

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

What is the UTC/GMT time of the following?

2015–06–20T07:50+02:00[Europe/Paris]

A

5:50

subtract the second time from the first in order to get the UTC/GMT time

If it was a -02:00 it’s like subtracting a negative, so adding

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

How to create a LocalDate object for January 20th, 2015

A

LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);

or

LocalDate date2 = LocalDate.of(2015, 1, 20);

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

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

How to create a LocalTime object for 6:15 AM

A

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

You can also go further and specify seconds and nanoseconds:

LocalTime time2 = LocalTime.of(6, 15, 30);
LocalTime time3 = LocalTime.of(6, 15, 30, 200);

public static LocalTime of(int hour, int minute)
public static LocalTime of(int hour, int minute, int second)
public static LocalTime of(int hour, int minute, int second, int nanos)

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

Method signatures for creating a LocalDateTime object

A

You always need to include the year, month, and day of month as the first three parameters, but then you can do any level of specification of the time parameters from hour and minute down to nanoseconds.

You can also do of(LocalDate date, LocalTime time)

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

Method signatures for creating a ZonedDateTime object

A

public static ZonedDateTime of(int year, int month,
int dayOfMonth, int hour, int minute, int second, int nanos, ZoneId zone)

public static ZonedDateTime of(LocalDate date, LocalTime time, ZoneId zone)

public static ZonedDateTime of(LocalDateTime dateTime, ZoneId zone)

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

How to create a ZoneId object for the US/Eastern time zone

A

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

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

What happens if you try to do this?

LocalDate.of(2015, Month.JANUARY, 32)

A

Throws a DateTimeException

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

How to create a LocalDate, LocalTime, or LocalDateTime object with the current date or time

A

LocalDate.now()
LocalTime.now()
LocalDateTime.now()

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

What are the methods used to manipulate a LocalDate or LocalDateTime object?

A

plusDays(long days)
plusWeeks(long weeks)
plusMonths(long months)
plusYears(long years)

minusDays(long days)
minusWeeks(long weeks)
minusMonths(long months)
minusYears(long years)

Note: LocalDate and LocalDateTime are immutable so you must assign this manipulated LocalDate object to itself, such as

date = date.plusDays(1);

These methods can also be chained.

date = date.plusDays(1).plusWeeks(1);

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

What date is contained in the LocalDate object after this code is run?

LocalDate date = LocalDate.of(2014, Month.JANUARY, 30);
date = date.plusMonths(1);

A

2014-02-28

Java knows that there aren’t 30 days in February and rounds it down appropriately.

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

What are the methods used to manipulate a LocalTime or LocalDateTime object?

A

plusHours(long hours)
plusMinutes(long minutes)
plusSeconds(long seconds)
plusNanos(long nanoseconds)

minusHours(long hours)
minusMinutes(long minutes)
minusSeconds(long seconds)
minusNanos(long nanoseconds)

Note: LocalTime and LocalDateTime are immutable so you must assign this manipulated object to itself, such as

time = time.plusHours(1);

These methods can also be chained.

time = time.plusHours(1).plusMinutes(1);

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

How to check if LocalDate object is a date before another

A

date1.isBefore(date2);

Can also be used for LocalTime and LocalDateTime

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

Period

A

A Period object holds a specified amount of years/months/days

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

5 ways to create a Period object

A
Period.ofYears(int years)
Period.ofMonths(int months)
Period.ofWeeks(int weeks)
Period.ofDays(int days)
Period.of(int years, int months, int days)

Note: You cannot chain these methods! If you need to do some combo of years/months/days, you have to use the last method

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

What is output by the following?

System.out.printIn(Period.of(1,2,3));

System.out.println(Period.ofMonths(3));

System.out.println(Period.ofWeeks(3));

A

P1Y2M3D

P3M

P21D

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

Duration

A

Works like a Period, but you can specify days, hours, minutes, seconds, or nanoseconds.

The way the time is actually stored is in hours, minutes, and seconds.

Note: Even though there is an ofDays method for Duration, you can’t add a Duration to a LocalDate because it stores the data only in terms of hours, minutes, and seconds

22
Q

Instant

A

Represents a specific moment in time in the GMT time zone

Instant now = Instant.now();

You can also convert a ZonedDateTime to an Instant

Instant instant = zonedDateTime.toInstant();

23
Q

Which units of time/days can you add to an Instant object?

A

Any unit day or smaller

Instant nextDay = instant.plus(1, ChronoUnit.DAYS);

Instant nextHour = instant.plus(1, ChronoUnit.HOURS);

24
Q

If you are updating a value in a loop, should you use a String, StringBuilder, or StringBuffer?

A

StringBuilder (because it’s mutable)

25
Q

If multiple threads are updating a value should you use String, StringBuilder, or StringBuffer?

A

StringBuffer

26
Q

What does the following print?

Locale locale = Locale.getDefault(); System.out.println(locale);

A

The user’s current locale.

In my case it would be “en_US”

27
Q

What are the two formats that a Locale can have?

A

1) Lowercase language only, such as “en”

2) Lowercase language, underscore, uppercase country, such as “en_US”

28
Q

How to create a Locale object for the a language (no constructor)?
For a country (no constructor)?
For a language using a constructor?
For a country using a constructor?

A

Locale.LANGUAGE_NAME
Locale.COUNTRY_NAME

new Locale(languageName)
new Locale(languageName, countryName);
29
Q

How to use the Locale builder to make a Locale object of “en_US”

A

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

30
Q

How to change the default locale to French (“fr”) for within a Java program?

A
Locale locale = new Locale("fr");
Locale.setDefault(locale);
31
Q

Resource Bundle

A

Contains the local specific objects to be used by a program. It is like a map with keys and values. The resource bundle can be in a property file or in a Java class.

32
Q

Property File

A

A file in a specific format with key/value pairs.
Only allows String keys/values

Named like: Bundlename_languagename.properties

example: Zoo_en.properties

33
Q

What are the three ways the key/value pairs can be formatted in a property file?

A

key=value
key:value
key value

34
Q

6 Property File Syntax Rules

A
  • If a line begins with # or !, it is a comment.
  • Spaces before or after the separator character are ignored.
  • Spaces at the beginning of a line are ignored.
  • Spaces at the end of a line are not ignored.
  • End a line with a backslash if you want to break the line for readability.
  • You can use normal Java escape characters like \t and \n.
35
Q

How to create a ResourceBundle object

A

For default locale:
ResourceBundle.getBundle(String bundleName)

To specify locale:
ResourceBundle.getBundle(String bundleName, Local locale)

36
Q

Method to turn a ResourceBundle object into a Set

A

Set keys = resourceBundle.keySet();

Then to print it you can use stream()…

keys.stream().map(k -> k + “ “ + rb.getString(k))
.forEach(System.out::println);

37
Q

Properties Class

A

An older alternative to ResourceBundle

You can convert from ResourceBundle to Properties like so

Properties props = new Properties(); 
rb.keySet().stream().forEach(k -> props.put(k, rb.getString(k)));
38
Q

What are the possible outputs for the following code?

Properties props = new Properties(); 
rb.keySet().stream().forEach(k -> props.put(k, rb.getString(k)));
System.out.println(props.getProperty("propertyName"));
System.out.println(props.getProperty("propertyName", "123"));
A

The first print statement will output null if no property exists with that key, or the value if the key does exist

The second print statement will output 123 if no property exists with that key, or the value if the key does exist

39
Q

What are the two main advantages of using a Java class instead of a property file for a resource bundle?

A
  • You can use a value type that is not a String.

- You can create the values of the properties at runtime.

40
Q

Given that the default locale is US English, what is the order of resource bundles that Java will attempt to find with the following code:

ResourceBundle.getBundle(“Zoo”, new Locale(“fr”, “FR”))

A

1) Zoo_fr_FR.java
2) Zoo_fr_FR.properties
3) Zoo_fr.java
4) Zoo_fr.properties
5) Zoo_en_US.java (the default locale)
6) Zoo_en_US.properties
7) Zoo_en.java
8) Zoo_en.properties
9) Zoo.java
10) Zoo.properties
11) If still not found, throw MissingResourceException.

41
Q

NumberFormat

A

Helps you to format and parse numbers for any locale.

42
Q

1) NumberFormat.getInstance()

2) NumberFormat.getInstance(locale)

A

1) Returns a general-purpose number format for the current default locale.
2) Returns a general-purpose number format for the specified locale.

Note: getNumberInstance() does the same thing

43
Q

1) NumberFormat.getCurrencyInstance()

2) NumberFormat.getCurrencyInstance(locale)

A

1) Returns a currency format for the current default locale.

2) Returns a currency format for the specified locale.

44
Q

1) NumberFormat.getPercentInstance()

2) NumberFormat.getPercentInstance(locale)

A

1) Returns a percentage format for the current default locale.
2) Returns a percentage format for the specified locale.

45
Q

NumberFormat format() method

A

Call this method on a NumberFormat object with a number as an argument to format it in the specified locale that is saved in the NumberFormat object.

46
Q

NumberFormat parse() method

A

Parses text from the beginning of the given string to produce a number. Returns a Number object.

The parse method parses only the beginning of a string. After it reaches a character that cannot be parsed, the parsing stops and the value is returned.

Note: Throws the checked exception ParseException if it fails to parse.

NumberFormat nf = NumberFormat.getInstance();
String one = “456abc”;
String two = “-2.5165x10”;
String three = “x85.3”;
System.out.println(nf.parse(one)); // 456 System.out.println(nf.parse(two)); // -2.5165 System.out.println(nf.parse(three));// throws ParseException

47
Q

What are some differences between short and medium DateTimeFormatters?

A

Short format shows the date/time in the DD/MM/YY HH:MM AM/PM format

Medium will write out the shortened version of the month and show the seconds and the full year, something like Jan 20, 2020 11:12:34 AM

48
Q

What method do you use to create your own DateTimeFormatter?

A

DateTimeFormatter.ofPattern()

49
Q

ZoneID.getAvailableZoneIds()

A

Returns a set of all possible time zone IDs

50
Q

ZoneID.systemDefault()

A

Returns your current ZoneId

51
Q

Given:

LocalDateTime dateTime = LocalDateTime.of(date, time);
DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

How would you call the formatter?

A

shortDateTime.format(dateTime);

OR

dateTime.format(shortDateTime);

The LocalDateTime and DateTimeFormatter objects can be in either order since they both define the format method