Java Date, Calendar and Time: Exercises, Practice, Solution
This resource offers a total of 230 Java Date, Time and Calendar problems for practice. It includes 46 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
1. Create Date Using Calendar
Write a Java program to create a Date object using the Calendar class.
2. Get Calendar Date Info
Write a Java program to get and display information (year, month, day, hour, minute) about a default calendar.
3. Get Calendar Max Values
Write a Java program to get the maximum value of the year, month, week, and date from the current date of a default calendar.
4. Get Calendar Min Values
Write a Java program to get the minimum value of year, month, week, date from the current date of a default calendar.
5. Get Time in New York
Write a Java program to get the current time in New York.
6. Get Current Date and Time
Write a Java program to get the current date and time.
7. Last Day of Current Month
Write a Java program to get the last day of the current month.
8. Last Date of Month
Write a Java program to get the last date of the month.
9. First and Last Day of Week
Write a Java program to calculate the first and last day of each week.
10. First and Last Day of Month
Write a Java program to get the first and last day of a month.
11. Days in Month
Write a Java program to get the number of days in a month.
12. Localized Day Names
Write a Java program to get localized day-of-week names.
13. Day of Week for Date
Write a Java program to get a day of the week on a specific date.
14. Get Current Local Time
Write a Java program to get the current local time.
15. Add Hours to Current Time
Write a Java program to add hours to the current time.
16. Date After Two Weeks
Write a Java program to get a date after 2 weeks.
17. Date Before and After Year
Write a Java program to get a date before and after 1 year and compare it to the current date.
18. Check Leap Year
Write a Java program to check if a year is a leap year or not.
19. Get Year and Month Between Dates
Write a Java program to get the year and month between two dates.
20. Get Current Timestamp
Write a Java program to get the current timestamp.
21. Get Time in All Time Zones
Write a Java program to get the current time in all time zones.
22. Dates 10 Days Before and After
Write a Java program to get the dates 10 days before and after today.
23. Months Left in Year
Write a Java program to get the number of months left in the year.
24. Format Dates and Times
Write a Java program to display dates in the following formats.
Sample format :
Default format of LocalDate=2016-09-16 16::Sep::2016 Default format of LocalDateTime=2016-09-16T11:46:01.455 16::Sep::2016 11::46::01 Default format of Instant=2016-09-16T06:16:01.456Z Default format after parsing = 2014-04-27T21:39:48
25. Year Info
Write a Java program to get the information of the current/given year.
Sample format :
Sample Output : Current Year: 2001 Is current year leap year? false Length of the year: 365 days
26. Month Info
Write a Java program to get the information of the current/given month.
Sample format :
Sample Output : Integer value of the current month: 2 Length of the month: 28 Maximum length of the month: 29 First month of the Quarter: JANUARY
27. Time Info
Write a Java program to get information about a given time.
28. DateTime Before Hours and Minutes
Wrtie a Java program that displays the date time information before a certain number of hours and minutes from the current date.
29. Convert String to Date
Write a Java program to convert a string to a date.
30. Date Difference (Y/M/D)
Write a Java program to compute the difference between two dates (years, months, days).
31. Date Difference (Time Units)
Write a Java program to compute the difference between two dates in hours, minutes, milliseconds, and nanoseconds.
32. Calculate Age
Write a Java program to calculate your age.
33. Next and Previous Fridays
Write a Java program to get the next and previous Fridays.
34. Today at Midnight
Write a Java program to get today's date at midnight time.
35. Extract Date and Time
Write a Java program to extract date and time from the date string.
36. Unix Timestamp to Date
Write a Java program to convert a Unix timestamp to a date.
37. Seconds Since 1970
Write a Java program to find seconds since 1970.
38. Days Between Two Dates
Write a Java program to calculate the difference between two dates in days.
39. Convert Between Date and String
Write a Java program to convert String to date and time and vice versa.
40. Date Only and Time Only
Write a Java program to display current date without time and current time without date.
41. Combine Local Date and Time
Write a Java program to combine local date and time into a single object.
42. Define Period and Duration
Write a Java program to define a period of time using date-based values (Period) and a duration of time using time-based values (Duration).
43. Show All Time Zones
Write a Java program to display all the available time zones in UTC and GMT.
44. Define and Extract Zone Offsets
Write a Java program to define and extract zone offsets.
45. Print Custom Date Formats
Write a Java program to print yyyy-MM-dd, HH:mm:ss, yyyy-MM-dd HH:mm:ss, E MMM yyyy HH:mm:ss.SSSZ and HH:mm:ss,Z format pattern for date and time.
46. Count Days Between Years
Write a Java program to count the number of days between two years.
Java Code Editor
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.