Use the following problems to test your date and time programming prowess. I strongly encourage you to give each problem a try before you turn to the solutions and download the example programs:
- Converting a string to date and time: Write a program that exemplifies conversions between a string and date/time.
- Formatting date and time: Explain the format pattern for date and time.
- Getting the current date/time without time/date: Write a program that extracts the current date without the time or date.
- LocalDateTime from LocalDate and LocalTime: Write a program that builds a LocalDateTime from LocalDate object and LocalTime. It combines the date and time in a single LocalDateTime object.
- Machine time via an Instant class: Explain and give an example of the Instant API.
- Defining a period of time using date-based values (Period) and a duration of time using time-based values (Duration): Explain and give an example of the usage of the Period and Duration APIs.
- Getting date and time units: Write a program that extracts the date and time units (for example, extract from date the year, month, minute, and so on) from an object representing a date-time.
- Adding and subtracting to/from a date-time: Write a program that adds (and subtracts) an amount of time (for example, years, days, or minutes) to a date-time object (for example, add an hour to date, subtract 2 days from LocalDateTime, and so on).
- Getting all time zones with UTC and GMT: Write a program that displays all the available time zones with UTC and GMT.
- Getting the local date-time in all available time zones: Write a program that displays the local time in all the available time zones.
- Displaying date-time information about a flight: Write a program that displays information about a scheduled flight time of 15 hours and 30 minutes. More precisely, a flight from Perth, Australia to Bucharest, Europe.
- Converting a Unix timestamp to date-time: Write a program that converts a Unix timestamp to java.util.Date and java.time.LocalDateTime.
- Finding the first/last day of the month: Write a program that finds the first/last day of the month via JDK 8, TemporalAdjusters.
- Defining/extracting zone offsets: Write a program that reveals different techniques for defining and extracting zone offsets.
- Converting between Date and Temporal: Write a program that converts between Date and Instant, LocalDate, LocalDateTime, and so on.
- Iterating a range of dates: Write a program that iterates a range of given dates, day by day (with a step of a day).
- Calculating age: Write a program that calculates the age of a person.
- Start and end of a day: Write a program that returns the start and end time of a day.
- Difference between two dates: Write a program that calculates the amount of time, in days, between two dates.
- Implementing a chess clock: Write a program that implements a chess clock.