Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Date, Calendar and Time: Exercises, Practice, Solution

Java Date, Time and Calendar Exercises [46 exercises with solution]

1. Write a Java program to create a Date object using the Calendar class. Go to the editor

Click me to see the solution

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

Click me to see the solution

3. Write a Java program to get the maximum value of the year, month, week, date from the current date of a default calendar. Go to the editor

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

12. Write a Java program to get localized day-in-week name. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

17. Write a Java program to get a date before and after 1 year compares to the current date. Go to the editor

Click me to see the solution

18. Write a Java program to check a year is a leap year or not. Go to the editor

Click me to see the solution

19. Write a Java program to get year and months between two dates. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

23. Write a Java program to get the months remaining in the year. Go to the editor

Click me to see the solution

24. Write a Java program to display the dates in the following formats. Go to the editor

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 current/given year. Go to the editor

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 current/given month. Go to the editor

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 the information of a given time. Go to the editor

Click me to see the solution

28. Write a Java program to display the date time information before some hours and minutes from current date time. Go to the editor

Click me to see the solution

29. Write a Java program to convert a string to date. Go to the editor

Click me to see the solution

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

Click me to see the solution

31. Write a Java program to compute the difference between two dates (Hours, minutes, milli, seconds and nano). Go to the editor

Click me to see the solution

32. Write a Java program to calculate your age. Go to the editor

Click me to see the solution

33. Write a Java program to get the next and previous Friday. Go to the editor

Click me to see the solution

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

Click me to see the solution

35. Write a Java program to extract date, time from the date string. Go to the editor

Click me to see the solution

36. Write a Java program to convert a unix timestamp to date in Java. Go to the editor

Click me to see the solution

37. Write a Java program to find seconds since 1970. Go to the editor

Click me to see the solution

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

Click me to see the solution

39. Write a java program to convert String to date and time and vice a versa. Go to the editor

Click me to see the solution

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

Click me to see the solution

41. Write a Java program to display combine local date and time in a single object. Go to the editor

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). Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

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

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.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe