Practice 70 Java Date Time Programming Questions, TechnoVlogs

Practice 70 Java Date Time Programming Questions


Q1. Write a Java program to get the current date and time.
  - Expected Output: Current date and time in the format "yyyy-MM-dd HH:mm:ss"

Q2. Write a Java program to get the current date.
  - Expected Output: Current date in the format "yyyy-MM-dd"

Q3. Write a Java program to get the current time.
  - Expected Output: Current time in the format "HH:mm:ss"

Q4. Write a Java program to display the current day of the week.
  - Expected Output: Current day of the week (e.g., "Monday")

Q5. Write a Java program to get the current month.
  - Expected Output: Current month (e.g., "October")

Q6. Write a Java program to check if a given year is a leap year.
  - Input: 2024
  - Expected Output: True

Q7. Write a Java program to find the number of days between two dates.
  - Input: Start date: "2023-01-01", End date: "2023-12-31"
  - Expected Output: 364

Q8. Write a Java program to find the difference in hours between two times.
  - Input: Start time: "10:00", End time: "15:30"
  - Expected Output: 5 hours 30 minutes

Q9. Write a Java program to get the current time in milliseconds.
  - Expected Output: Current time in milliseconds

Q10. Write a Java program to get the current date and time in a specific time zone.
   - Input: Time zone: "Asia/Kolkata"
   - Expected Output: Current date and time in "Asia/Kolkata"

Q11. Write a Java program to format a date in the "dd/MM/yyyy" format.
   - Input: "2023-10-15"
   - Expected Output: "15/10/2023"

Q12. Write a Java program to parse a date from a string.
   - Input: "2023-10-15", Format: "yyyy-MM-dd"
   - Expected Output: Date object representing "2023-10-15"

Q13. Write a Java program to compare two dates.
   - Input: Date1: "2023-10-15", Date2: "2023-10-16"
   - Expected Output: Date2 is later than Date1

Q14. Write a Java program to add days to a given date.
   - Input: Date: "2023-10-15", Days to add: 5
   - Expected Output: "2023-10-20"

Q15. Write a Java program to subtract days from a given date.
   - Input: Date: "2023-10-15", Days to subtract: 5
   - Expected Output: "2023-10-10"

Q16. Write a Java program to get the day of the year from a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: 288 (based on the number of days)

Q17. Write a Java program to get the week of the year from a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: 42

Q18. Write a Java program to check if a given date is a valid date.
   - Input: Date: "2023-02-29"
   - Expected Output: False (since 2023 is not a leap year)

Q19. Write a Java program to get the number of days in a month.
   - Input: Month: "February", Year: 2023
   - Expected Output: 28

Q20. Write a Java program to get the current date and time in "yyyy-MM-dd HH:mm:ss" format.
   - Expected Output: Current date and time in the format "yyyy-MM-dd HH:mm:ss"

Q21. Write a Java program to convert a date string to a different format.
   - Input: Date: "2023-10-15", From format: "yyyy-MM-dd", To format: "MM/dd/yyyy"
   - Expected Output: "10/15/2023"

Q22. Write a Java program to find the first day of the month for a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: "2023-10-01"

Q23. Write a Java program to get the last day of the month for a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: "2023-10-31"

Q24. Write a Java program to find the next Monday after a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: "2023-10-16"

Q25. Write a Java program to find the previous Monday before a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: "2023-10-09"

Q26. Write a Java program to get the number of days in a specific year.
   - Input: Year: 2024
   - Expected Output: 366 (since 2024 is a leap year)

Q27. Write a Java program to get the first date of the current week.
   - Expected Output: First date of the current week (e.g., "2023-10-09")

Q28. Write a Java program to get the last date of the current week.
   - Expected Output: Last date of the current week (e.g., "2023-10-15")

Q29. Write a Java program to add months to a given date.
   - Input: Date: "2023-10-15", Months to add: 2
   - Expected Output: "2023-12-15"

Q30. Write a Java program to subtract months from a given date.
   - Input: Date: "2023-10-15", Months to subtract: 2
   - Expected Output: "2023-08-15"

Q31. Write a Java program to add years to a given date.
   - Input: Date: "2023-10-15", Years to add: 3
   - Expected Output: "2026-10-15"

Q32. Write a Java program to subtract years from a given date.
   - Input: Date: "2023-10-15", Years to subtract: 3
   - Expected Output: "2020-10-15"

Q33. Write a Java program to find the age in years given a birth date.
   - Input: Birthdate: "1990-05-15"
   - Expected Output: Age in years (e.g., 33)

Q34. Write a Java program to get the current date and time in a different time zone.
   - Input: Time zone: "America/New_York"
   - Expected Output: Current date and time in "America/New_York"

Q35. Write a Java program to convert a date string to a Date object.
   - Input: "2023-10-15", Format: "yyyy-MM-dd"
   - Expected Output: Date object representing "2023-10-15"

Q36. Write a Java program to convert a Date object to a string in the format "MM/dd/yyyy".
   - Input: Date: "2023-10-15"
   - Expected Output: "10/15/2023"

Q37. Write a Java program to check if a given date is before the current date.
   - Input: Date: "2023-10-10"
   - Expected Output: True (if today's date is later)

Q38. Write a Java program to check if a given date is after the current date.
   - Input: Date: "2023-10-20"
   - Expected Output: True (if today's date is earlier)

Q39. Write a Java program to find the difference in days between two dates.
   - Input: Start date: "2023-10-10", End date: "2023-10-15"
   - Expected Output: 5 days

Q40. Write a Java program to format the current date in "dd-MM-yyyy" format.
   - Expected Output: Current date in "dd-MM-yyyy" format

Q41. Write a Java program to find the date after adding 10 days to the current date.
   - Expected Output: Date 10 days after today's date

Q42. Write a Java program to find the date before subtracting 10 days from the current date.
   - Expected Output: Date 10 days before today's date

Q43. Write a Java program to convert a timestamp to a date.
   - Input: Timestamp: 1632905600000
   - Expected Output: Date corresponding to timestamp

Q44. Write a Java program to get the current time in "HH:mm:ss" format.
   - Expected Output: Current time in "HH:mm:ss" format

Q45. Write a Java program to calculate the number of seconds between two times.
   - Input: Start time: "10:00:00", End time: "12:00:00"
   - Expected Output: 7200 seconds

Q46. Write a Java program to add minutes to a given time.
   - Input: Time: "10:00", Minutes to add: 30
   - Expected Output: "10:30"

Q47. Write a Java program to subtract minutes from a given time.
   - Input: Time: "10:00", Minutes to subtract: 30
   - Expected Output: "09:30"

Q48. Write a Java program to get the current date and time in ISO 8601 format.
   - Expected Output: Current date and time in ISO 8601 format ("yyyy-MM-dd'T'HH:mm:ss")

Q49. Write a Java program to get the day of the week from a given date.
   - Input: Date: "2023-10-15"
   - Expected Output: "Sunday"

Q50. Write a Java program to get the first day of the current year.
   - Expected Output: First day of the current year (e.g., "2023-01-01")

Q51. Write a Java program to check if a date is before or after a specified date.
   - Input: Date1: "2023-10-10", Date2: "2023-10-15"
   - Expected Output: Date1 is before Date2

Q52. Write a Java program to get the number of days in the current month.
   - Expected Output: Number of days in the current month

Q53. Write a Java program to check if a given time is in the AM or PM.
   - Input: Time: "15:30"
   - Expected Output: PM

Q54. Write a Java program to get the current time in a 12-hour format with AM/PM.
   - Expected Output: Current time in 12-hour format (e.g., "03:30 PM")

Q55. Write a Java program to format the current date to display only the year.
   - Expected Output: Current year in "yyyy" format

Q56. Write a Java program to find the current day of the month.
   - Expected Output: Current day of the month (e.g., 15)

Q57. Write a Java program to add seconds to a given time.
   - Input: Time: "10:00", Seconds to add: 45
   - Expected Output: "10:00:45"

Q58. Write a Java program to check if a given date is in the future.
   - Input: Date: "2023-10-20"
   - Expected Output: True (if today's date is before the input date)

Q59. Write a Java program to find the current timestamp.
   - Expected Output: Current timestamp in milliseconds

Q60. Write a Java program to find the first Sunday of the current year.
   - Expected Output: Date of the first Sunday of the current year

Q61. Write a Java program to add days, months, and years to a date.
   - Input: Date: "2023-10-15", Add days: 5, months: 2, years: 1
   - Expected Output: "2024-12-20"

Q62. Write a Java program to get the current date and time in the format "MM-dd-yyyy HH:mm:ss".
   - Expected Output: Current date and time in "MM-dd-yyyy HH:mm:ss"

Q63. Write a Java program to find the first and last date of the current quarter.
   - Expected Output: First and last date of the current quarter

Q64. Write a Java program to display the current date in the "yyyyMMdd" format.
   - Expected Output: Current date in "yyyyMMdd" format

Q65. Write a Java program to subtract months and years from a given date.
   - Input: Date: "2023-10-15", Months to subtract: 3, Years to subtract: 2
   - Expected Output: "2021-07-15"

Q66. Write a Java program to get the current time in "HH:mm:ss.SSS" format.
   - Expected Output: Current time with milliseconds in "HH:mm:ss.SSS" format

Q67. Write a Java program to find the difference in minutes between two times.
   - Input: Start time: "10:00", End time: "12:30"
   - Expected Output: 150 minutes

Q68. Write a Java program to add or subtract days from the current date based on user input.
   - Input: Days to add: 10
   - Expected Output: Date 10 days from today

Q69. Write a Java program to check if the current time is between two given times.
   - Input: Start time: "09:00", End time: "17:00"
   - Expected Output: True or False depending on the current time

Q70. Write a Java program to display the current date and time in "EEEE, MMM dd, yyyy HH:mm:ss" format.
   - Expected Output: Current date and time in "EEEE, MMM dd, yyyy HH:mm:ss" format

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!