3 Dates & Times Flashcards
List the three ways to create current date and/or time.
LocalDate.now(); //today’s date
LocalTime.now(); // time right now
LocalDateTime.now(); // date and time right now
What is the method signature and an example to create:
1) a specific time (hrs & minutes, hrs minutes and seconds)
2) a specific date
LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
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)
LocalTime time1 = LocalTime.of(6, 15); // hour and minute
LocalTime time2 = LocalTime.of(6, 15, 30); // + seconds
LocalTime time3 = LocalTime.of(6, 15, 30, 200); // + nanoseconds
LocalDateTime dateTime1 = LocalDateTime.of(2015, Month.JANUARY, 20, 6, 15, 30);
LocalDateTime dateTime2 = LocalDateTime.of(date1, time1);
What is the import statement to work with date & time?
import java.time.*; // import time classes
What are the methods to increase/decrease years, months, days, weeks, hours, minutes, seconds?
12: LocalDate date = LocalDate.of(2014, Month.JANUARY, 20);
13: System.out.println(date); // 2014-01-20
14: date = date.plusDays(2);
15: System.out.println(date); // 2014-01-22
16: date = date.plusWeeks(1);
continued
Working with Dates and Times 143
17: System.out.println(date); // 2014-01-29
18: date = date.plusMonths(1);
19: System.out.println(date); // 2014-02-28
20: date = date.plusYears(5);
21: System.out.println(date); // 2019-02-28
22: LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
23: LocalTime time = LocalTime.of(5, 15);
24: LocalDateTime dateTime = LocalDateTime.of(date, time);
25: System.out.println(dateTime); // 2020-01-20T05:15
26: dateTime = dateTime.minusDays(1);
27: System.out.println(dateTime); // 2020-01-19T05:15
28: dateTime = dateTime.minusHours(10);
29: System.out.println(dateTime); // 2020-01-18T19:15
30: dateTime = dateTime.minusSeconds(30);
31: System.out.println(dateTime); // 2020-01-18T19:14:30
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(5, 15);
LocalDateTime dateTime = LocalDateTime.of(date2, time)
.minusDays(1).minusHours(10).minusSeconds(30);
Dates are immutable, like Strings. What does that mean?
Value isn’t changed unless there is an assignment statement.
date.plusDays(10); // doesn’t change the date
date=date.plusDays(10); // this changes it
Is this correct? LocalDate now = new LocalDate();
No, it’s a static method. There is no constructor.
the correct way is LocalDate now = LocalDate.now();
What does the period class do? List the 5 signatures.
Adds a specified period of time
Period annually = Period.ofYears(1); // every 1 year
Period quarterly = Period.ofMonths(3); // every 3 months
Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks
Period everyOtherDay = Period.ofDays(2); // every 2 days
Period everyYearAndAWeek = Period.of(1, 0, 7); // every year and 7 days
How many ways are there to create a period class?
5
Can periods be chained?
You cannot chain methods when CREATING a Period. . Only the last method is used because the Period.ofXXX methods are static methods. It’s legal, won’t throw an error but only the last method will get implemented.
Period wrong = Period.ofYears(1).ofWeeks(1); // only weeks is used.
You CAN chain periods when altering a value. You just can’t use chains when creating a PERIOD.
date1=date1.plus(period1).plus(period2); // ok to chain
What are the methods to get these individual units out of a date/time object ?
- Day of the week
- the month
- the year
- the Day of the year
- Day of the week date.getDayOfWeek()
- the month date.getMonth()
- the year date.getYear()
- the Day of the year date.getDayOfYear()
What does the class DateTimeFormatter do?
Java provides a class called DateTimeFormatter to help us out. Unlike the LocalDateTime class, DateTimeFormatter can be used to format any type of date and/or time object. What changes is the format.
What pakage is the class DateTimeFormatter in?
java.time.format.
Give some examples of how to write methods for formatting date, time, and dateTime
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(11, 12, 34);
LocalDateTime dateTime = LocalDateTime.of(date, time);System.out.println(date
.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME));
System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
prints
2020-01-20
11:12:34
2020-01-20T11:12:34
How can formatters be set up?
DateTimeFormatter shortDateTime =
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(shortDateTime.format(dateTime)); // 1/20/20
System.out.println(shortDateTime.format(date)); // 1/20/20
DateTimeFormatter shortDateTime =
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
System.out.println(dateTime.format(shortDateTime));
System.out.println(date.format(shortDateTime));
System.out.println(time.format(shortDateTime));
What are the predefined formats for date and time in the DateTimeFormatter class.
There are two predefi ned formats that can show up on the exam: SHORT and MEDIUM. The other predefined formats involve time zones, which are not on the exam.
LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
LocalTime time = LocalTime.of(11, 12, 34);
LocalDateTime dateTime = LocalDateTime.of(date, time);
DateTimeFormatter shortF = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT);
DateTimeFormatter mediumF = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(shortF.format(dateTime)); // 1/20/20 11:12 AM
System.out.println(mediumF.format(dateTime)); // Jan 20, 2020 11:12:34 AM