3 Dates & Times Flashcards
(23 cards)
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
Custom formats
DateTimeFormatter f = DateTimeFormatter.ofPattern(“MMMM dd, yyyy, hh:mm”);
System.out.println(dateTime.format(f)); // January 20, 2020, 11:12
- MMMM M represents the month.
- The more Ms you have, the more verbose the Java output.
- For example, M outputs 1,
- MM outputs 01,
- MMM outputs Jan,
- MMMM outputs January.
dd d represents the date in the month. As with month, the more ds you have, the more verbose the Java output.
- dd means include the leading zero for a single-digit month.
-Use , if you want to output a comma (this also appears after the year).
-yyyy y represents the year.
-yy outputs a two-digit year and yyyy outputs a four-digit year.
hh h represents the hour. Use hh to include the leading zero if you’re outputting a singledigit hour. : Use : if you want to output a colon.
mm m represents the minute.
Parsing Dates and Times
The parse method turns a String into a date or time object. It requires two parameters:
1) String
2) formatter.
The parse() method takes a formatter as well. If you don’t specify one, it uses the default for that type. DateTimeFormatter f = DateTimeFormatter.ofPattern("MM dd yyyy"); LocalDate date = LocalDate.parse("01 02 2015", f); LocalTime time = LocalTime.parse("11:22"); System.out.println(date); // 2015-01-02 System.out.println(time); // 11:22
Is this correct? Period quarterly = new Period();
No, a static method must be used.
This is correct:
Period quarterly = Period.ofMonths(3);
Some common STATIC methods for date/time
Period period = Period.between(date1, date2);
Can a local variable be used outside the method?
No.
How are local variables affected by code blocks (Braces { } )?
Remember that blocks can contain other blocks. These smaller contained blocks can reference
variables defined in the larger scoped blocks, but not vice versa.
How do you need to write the date String as a parameter if you are NOT using a formatter?
“year-month-day”
Ex: “2015-03-25” including the dashes
LocalDate date = LocalDate.parse(“2015-02-01”);
LocalDate.parse(“2015 02 01”); //will not compile
LocalDate.parse(“01-02-2015”); //will not compile
You need to add a formatter if the String will be different so the compiler knows what’s what in the incoming String.
DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate date = LocalDate.parse(“01 02 2015”);
What’s the difference between MM and mm?
MM - months
mm - minutes