Chapter 5: Dates and Time Flashcards

1
Q

Convert to UTC

A

UTC is co-ordinated universal time (same as GMT). To convert to UTC you subtract the offset. E.g. offset is +2 you subtract 2. If offset is -2 then add 2.

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

Immutability

A

All date and time classes are immutable, so you need to reassign to a new variable if you want to change a value.. Also watch out for chaining methods.

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

Epoch

A

Epoch is date since January 1 1970 GMT. .toEpochDay() is days since then. toEpochSecond() is seconds since then.

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

Period

A

.of(Y, M, D) Display is P 1Y 1M 1D. P is mandatory. For others if they are 0 they are ommitted. Period doesn’t have weeks. Also can’t use period with just Time objects.

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

Duration

A

Intended for smaller units of time.
Starts with PT.
Can have duration of days, hours, minutes, seconds, millis, nanos, half_days.
factory method to construct is Days.of(x, ChronoUnit).
Where ChronoUnit has enum values of .DAYS, .MINUTES etc
Can’t use duration for just date objects, needs to have time in it.
Can use ChronoUnit.X.between(z, y) do see how much of a certain duration is between two date times.

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

Instants

A

.toInstant() converts to GMT. Java must have a zone to get instant. We can only use this with ZonedDateTime.
Instant.ofEpochSeconds(long seconds)

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

Daylight savings time

A

Spring forward in Spring and fall back in the fall.
If you try to set a time which doesn’t exist, it will be converted to the actual time +/- the correct offset in GMT (No exception is thrown - Java handles this for us!)

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

Locale

A

Allows strings and dates to be formatted correctly for that language/region.
Locale string formats: either language only e.g. fr or language and country e.g. en_US. Language must always come first in lower case.

Locale.ENUM has commonly used Locales. Or you can use the constructor or builder.
If you use an invalid locale, Java will accept it, but the program will not behave as expected.
.setDefault() sets the default Locale.

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

Resource bundle

A

Can be in a property file or in a java class. Has a specific format with key value pairs.
E.g. File_en.properties or File_fr.properties.
Can access the resource bundle via ResourceBundle rb = ResourceBundle.getBundle(“File”, “languageFromLocal”). Then use rb.getString(“key”) to get the value.

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

Formats in a resource bundle

A

key = value
key: value
key value

! are comments
Spaces before and after the seperator character are ignored.
Spaces at the beginning of the line are ignored
Spaces at end of line not ignored
End a line with a \ if you want to break for readability
Can use a normal Java escape character e.g. \t or \n.

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

Properties

A

Used before ResourceBundle. You call .get(“key”) or get.property(“key”, “defaultValue”) to retrieve values.

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

Creating a resource bundle in java

A

needs to have same format as properties file, except that it has .java on the end instead of.properties.

Implement method: protected Object[][] getContent(){}.
means that you don’t need to have the restriction of values being a String

ResourceBundle.getBundle(“packagename.Class”, “locale”)
Note, if you don’t specify a locale when creating the java class, it will use the programs default Locale.

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

Order of preference of bundles

A
  1. Class_fr_FR class + lang + country
  2. Class _fr class + lang
  3. Class _en_US class + defualt lang + default country
  4. Class_en class + default lang
  5. Class class

Always .java ahead of .properties in each rank!!

Once Java finds a match, it will look for keys in any child bundle e.g.
1. class + lang + country
2. class + lang
3. class 
BUT won't go and look in default.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Formatting numbers

A

NumberFormat.getInstance()
NumberFormat.getNumberInstance() same as standard
NumberFormat.getCurrencyInstance() used for formatting monetary amounts
NumberFormat.getPercentageInstance() for percentages
Can also pass in a locale.

Then call .format() to turn a number into a string
Then call .parse() to turn a String into a number.
If you’re using a currency instance it will use the currency as well.
When parsing, it can throw a checked exception e.g. ParseException. Make sure this is handled in the exam.
Java parses until it finds a character it cannot deal with and then returns the result. If the result is not a proper number it will throw a ParseException.
When you parse a String of £100 in a currency instance, it will turn it to a double of 100.

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

Formatting dates and time

A
SHORT 1/12/10 11:12AM
MEDIUM Jan 20, 2020 11L12l34 AM
M isfor month 
M = 1, MM = 01, MMM = Jan , MMMM = January
d is for day
y is for year
yy = 2 digit year
yyyy = 4 digit year
How well did you know this?
1
Not at all
2
3
4
5
Perfectly