w3resource

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.

Click me to see the solution

2. Write a Java program to get and display information (year, month, day, hour, minute) about a default calendar.

Click me to see the solution

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.

Click me to see the solution

4. Write a Java program to get the minimum value of year, month, week, date from the current date of a default calendar.

Click me to see the solution

5. Write a Java program to get the current time in New York.

Click me to see the solution

6. Write a Java program to get the current date and time.

Click me to see the solution

7. Write a Java program to get the last day of the current month.

Click me to see the solution

8. Write a Java program to get the last date of the month.

Click me to see the solution

9. Write a Java program to calculate the first and last day of each week.

Click me to see the solution

10. Write a Java program to get the first and last day of a month.

Click me to see the solution

11. Write a Java program to get the number of days in a month.

Click me to see the solution

12. Write a Java program to get localized day-of-week names.

Click me to see the solution

13. Write a Java program to get a day of the week on a specific date.

Click me to see the solution

14. Write a Java program to get the current local time.

Click me to see the solution

15. Write a Java program to add hours to the current time.

Click me to see the solution

16. Write a Java program to get a date after 2 weeks.

Click me to see the solution

17. Write a Java program to get a date before and after 1 year and compare it to the current date.

Click me to see the solution

18. Write a Java program to check if a year is a leap year or not.

Click me to see the solution

19. Write a Java program to get the year and month between two dates.

Click me to see the solution

20. Write a Java program to get the current timestamp.

Click me to see the solution

21. Write a Java program to get the current time in all time zones.

Click me to see the solution

22. Write a Java program to get the dates 10 days before and after today.

Click me to see the solution

23. Write a Java program to get the number of months left in the year.

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

27. Write a Java program to get information about a given time.

Click me to see the solution

28. Wrtie a Java program that displays the date time information before a certain number of hours and minutes from the current date.

Click me to see the solution

29. Write a Java program to convert a string to a date.

Click me to see the solution

30. Write a Java program to compute the difference between two dates (years, months, days).

Click me to see the solution

31. Write a Java program to compute the difference between two dates in hours, minutes, milliseconds, and nanoseconds.

Click me to see the solution

32. Write a Java program to calculate your age.

Click me to see the solution

33. Write a Java program to get the next and previous Fridays.

Click me to see the solution

34. Write a Java program to get today's date at midnight time.

Click me to see the solution

35. Write a Java program to extract date and time from the date string.

Click me to see the solution

36. Write a Java program to convert a Unix timestamp to a date.

Click me to see the solution

37. Write a Java program to find seconds since 1970.

Click me to see the solution

38. Write a Java program to calculate the difference between two dates in days.

Click me to see the solution

39. Write a Java program to convert String to date and time and vice versa.

Click me to see the solution

40. Write a Java program to display current date without time and current time without date.

Click me to see the solution

41. Write a Java program to combine local date and time into a single object.

Click me to see the solution

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).

Click me to see the solution

43. Write a Java program to display all the available time zones in UTC and GMT.

Click me to see the solution

44. Write a Java program to define and extract zone offsets.

Click me to see the solution

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.

Click me to see the solution

46. Write a Java program to count the number of days between two years.

Click me to see the solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/datetime/index.php