Chapter 5: Dates and Time Flashcards
Convert to UTC
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.
Immutability
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.
Epoch
Epoch is date since January 1 1970 GMT. .toEpochDay() is days since then. toEpochSecond() is seconds since then.
Period
.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.
Duration
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.
Instants
.toInstant() converts to GMT. Java must have a zone to get instant. We can only use this with ZonedDateTime.
Instant.ofEpochSeconds(long seconds)
Daylight savings time
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!)
Locale
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.
Resource bundle
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.
Formats in a resource bundle
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.
Properties
Used before ResourceBundle. You call .get(“key”) or get.property(“key”, “defaultValue”) to retrieve values.
Creating a resource bundle in java
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.
Order of preference of bundles
- Class_fr_FR class + lang + country
- Class _fr class + lang
- Class _en_US class + defualt lang + default country
- Class_en class + default lang
- 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.
Formatting numbers
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.
Formatting dates and time
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