Java Date, Calendar and Time: Exercises, Practice, Solution
Java Date, Time and Calendar Exercises [46 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Java program to create a Date object using the Calendar class.
2. Write a Java program to get and display information (year, month, day, hour, minute) about a default calendar.
3. 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. Write a Java program to get the minimum value of year, month, week, date from the current date of a default calendar.
5. Write a Java program to get the current time in New York.
6. Write a Java program to get the current date and time.
7. Write a Java program to get the last day of the current month.
8. Write a Java program to get the last date of the month.
9. Write a Java program to calculate the first and last day of each week.
10. Write a Java program to get the first and last day of a month.
11. Write a Java program to get the number of days in a month.
12. Write a Java program to get localized day-of-week names.
13. Write a Java program to get a day of the week on a specific date.
14. Write a Java program to get the current local time.
15. Write a Java program to add hours to the current time.
16. Write a Java program to get a date after 2 weeks.
17. Write a Java program to get a date before and after 1 year and compare it to the current date.
18. Write a Java program to check if a year is a leap year or not.
19. Write a Java program to get the year and month between two dates.
20. Write a Java program to get the current timestamp.
21. Write a Java program to get the current time in all time zones.
22. Write a Java program to get the dates 10 days before and after today.
23. Write a Java program to get the number of months left in the year.
24. 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. 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. 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. Write a Java program to get information about a given time.
28. Wrtie a Java program that displays the date time information before a certain number of hours and minutes from the current date.
29. Write a Java program to convert a string to a date.
30. Write a Java program to compute the difference between two dates (years, months, days).
31. Write a Java program to compute the difference between two dates in hours, minutes, milliseconds, and nanoseconds.
32. Write a Java program to calculate your age.
33. Write a Java program to get the next and previous Fridays.
34. Write a Java program to get today's date at midnight time.
35. Write a Java program to extract date and time from the date string.
36. Write a Java program to convert a Unix timestamp to a date.
37. Write a Java program to find seconds since 1970.
38. Write a Java program to calculate the difference between two dates in days.
39. Write a Java program to convert String to date and time and vice versa.
40. Write a Java program to display current date without time and current time without date.
41. Write a Java program to combine local date and time into a single object.
42. 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. Write a Java program to display all the available time zones in UTC and GMT.
44. Write a Java program to define and extract zone offsets.
45. 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. 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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics