Dates, Strings, and Localization Flashcards
Package where most date and time classes are
java.time
LocalDate
Contains just a date—no time and no time zone.
LocalTime
Contains just a time—no date and no time zone.
LocalDateTime
Contains both a date and time but no time zone.
ZonedDateTime
Contains a date, time, and time zone.
What is the UTC/GMT time of the following?
2015–06–20T07:50+02:00[Europe/Paris]
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 to create a LocalDate object for January 20th, 2015
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 to create a LocalTime object for 6:15 AM
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)
Method signatures for creating a LocalDateTime object
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)
Method signatures for creating a ZonedDateTime object
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 to create a ZoneId object for the US/Eastern time zone
ZoneId zone = ZoneId.of(“US/Eastern”);
What happens if you try to do this?
LocalDate.of(2015, Month.JANUARY, 32)
Throws a DateTimeException
How to create a LocalDate, LocalTime, or LocalDateTime object with the current date or time
LocalDate.now()
LocalTime.now()
LocalDateTime.now()
What are the methods used to manipulate a LocalDate or LocalDateTime object?
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);
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);
2014-02-28
Java knows that there aren’t 30 days in February and rounds it down appropriately.
What are the methods used to manipulate a LocalTime or LocalDateTime object?
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 to check if LocalDate object is a date before another
date1.isBefore(date2);
Can also be used for LocalTime and LocalDateTime
Period
A Period object holds a specified amount of years/months/days
5 ways to create a Period object
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
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));
P1Y2M3D
P3M
P21D