w3resource

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.

Click me to see the solution

2. Get Calendar Date Info

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

Click me to see the solution

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.

Click me to see the solution

5. Get Time in New York

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

Click me to see the solution

6. Get Current Date and Time

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

Click me to see the solution

7. Last Day of Current Month

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

Click me to see the solution

8. Last Date of Month

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

Click me to see the solution

9. First and Last Day of Week

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

Click me to see the solution

10. First and Last Day of Month

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

Click me to see the solution

11. Days in Month

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

Click me to see the solution

12. Localized Day Names

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

Click me to see the solution

13. Day of Week for Date

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

Click me to see the solution

14. Get Current Local Time

Write a Java program to get the current local time.

Click me to see the solution

15. Add Hours to Current Time

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

Click me to see the solution

16. Date After Two Weeks

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

Click me to see the solution

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.

Click me to see the solution

18. Check Leap Year

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

Click me to see the solution

19. Get Year and Month Between Dates

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

Click me to see the solution

20. Get Current Timestamp

Write a Java program to get the current timestamp.

Click me to see the solution

21. Get Time in All Time Zones

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

Click me to see the solution

22. Dates 10 Days Before and After

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

Click me to see the solution

23. Months Left in Year

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

27. Time Info

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

Click me to see the solution

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.

Click me to see the solution

29. Convert String to Date

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

Click me to see the solution

30. Date Difference (Y/M/D)

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

Click me to see the solution

31. Date Difference (Time Units)

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

Click me to see the solution

32. Calculate Age

Write a Java program to calculate your age.

Click me to see the solution

33. Next and Previous Fridays

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

Click me to see the solution

34. Today at Midnight

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

Click me to see the solution

35. Extract Date and Time

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

Click me to see the solution

36. Unix Timestamp to Date

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

Click me to see the solution

37. Seconds Since 1970

Write a Java program to find seconds since 1970.

Click me to see the solution

38. Days Between Two Dates

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

Click me to see the solution

39. Convert Between Date and String

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

Click me to see the solution

40. Date Only and Time Only

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

Click me to see the solution

41. Combine Local Date and Time

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

Click me to see the solution

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

Click me to see the solution

43. Show All Time Zones

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

Click me to see the solution

44. Define and Extract Zone Offsets

Write a Java program to define and extract zone offsets.

Click me to see the solution

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.

Click me to see the solution

46. Count Days Between Years

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.



Follow us on Facebook and Twitter for latest update.