Homework :
1) Write a program in java to accept a date in format DD/MM/YYYY and check whether the given date is valid or not.
import java.util.*;
public class SimpleDateValidator {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static boolean isValidDate(int day, int month, int year)
{
if (month < 1 || month > 12 || day < 1 || year < 1)
{
return false;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year))
{
daysInMonth[1] = 29;
}
return day <= daysInMonth[month-1];
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter date (DD/MM/YYYY): ");
String date = scanner.nextLine();
String[] parts = date.split("/");
if (parts.length != 3)
{
System.out.println("Invalid format! Use DD/MM/YYYY.");
return;
}
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (isValidDate(day, month, year))
System.out.println(date + " is a valid date.");
else
System.out.println(date + " is an invalid date.");
}
}
Output :
Enter date (DD/MM/YYYY): 12/05/2025
12/05/2025 is a valid date.
Enter date (DD/MM/YYYY): 29/02/2025
29/02/2025 is an invalid date.
2) Write a Java program to accept a date in format DD/MM/YYYY and find nth day.
import java.util.*;
public class FindNthDay
{
public static boolean isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static boolean isValidDate(int day, int month, int year)
{
if (month < 1 || month > 12 || day < 1 || year < 1)
{
return false;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year))
{
daysInMonth[1] = 29;
}
return day <= daysInMonth[month-1];
}
public static int getNthDay(int day, int month, int year)
{
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
{
daysInMonth[1] = 29;
}
int nthDay = 0;
for (int i = 0; i < month-1; i++) {
nthDay += daysInMonth[i];
}
nthDay += day;
return nthDay;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter date (DD/MM/YYYY): ");
String date = scanner.nextLine();
String[] parts = date.split("/");
if (parts.length != 3) {
System.out.println("Invalid format! Use DD/MM/YYYY.");
return;
}
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (isValidDate(day, month, year))
{
int nthDay = getNthDay(day, month, year);
System.out.println(date + " is a valid date and it's the " + nthDay + "th day of the year.");
}
else
{
System.out.println(date + " is an invalid date.");
}
}
}
Output :
Enter date (DD/MM/YYYY): 12/05/2025
12/05/2025 is a valid date and it's the 132th day of the year.
3) Write a program to accept the nth day and year and find the date.
import java.util.*;
public class FindDateFromNthDay {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static String getDateFromNthDay(int nthDay, int year) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
int month = 1, day = nthDay;
for (int i = 0; i < 12; i++) {
if (day <= daysInMonth[i]) {
break;
}
day -= daysInMonth[i];
month++;
}
String date = "";
if (day < 10)
{
date += "0";
}
date += day + "/";
if (month < 10)
{
date += "0";
}
date += month + "/" + year;
return date;
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the nth day: ");
int nthDay = scanner.nextInt();
System.out.print("Enter the year: ");
int year = scanner.nextInt();
if (nthDay < 1 || nthDay > (isLeapYear(year) ? 366 : 365))
{
System.out.println("Invalid nth day! Must be between 1 and " + (isLeapYear(year) ? 366 : 365));
return;
}
String date = getDateFromNthDay(nthDay, year);
System.out.println("The date corresponding to the " + nthDay + "th day of " + year + " is: " + date);
}
}
Output :
Enter the nth day: 60
Enter the year: 2025
The date corresponding to the 60th day of 2025 is: 01/03/2025
4) Write a program to accept two dates in DD/MM/YYYY format and find the difference between them of same
year.
import java.util.*;
public class DateDifferenceCalculator {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static boolean isValidDate(int day, int month, int year)
{
if (month < 1 || month > 12 || day < 1 || year < 1)
{
return false;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year))
{
daysInMonth[1] = 29;
}
return day <= daysInMonth[month - 1];
}
public static int getNthDay(int day, int month, int year)
{
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
{
daysInMonth[1] = 29;
}
int nthDay = 0;
for (int i = 0; i < month - 1; i++)
{
nthDay += daysInMonth[i];
}
nthDay += day;
return nthDay;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first date (DD/MM/YYYY): ");
String date1 = scanner.nextLine().trim();
System.out.print("Enter second date (DD/MM/YYYY): ");
String date2 = scanner.nextLine().trim();
String[] parts1 = date1.split("/");
String[] parts2 = date2.split("/");
if (parts1.length != 3 || parts2.length != 3) {
System.out.println("Invalid format! Use DD/MM/YYYY.");
return;
}
int day1 = Integer.parseInt(parts1[0]);
int month1 = Integer.parseInt(parts1[1]);
int year1 = Integer.parseInt(parts1[2]);
int day2 = Integer.parseInt(parts2[0]);
int month2 = Integer.parseInt(parts2[1]);
int year2 = Integer.parseInt(parts2[2]);
if (year1 != year2) {
System.out.println("Dates must be from the same year!");
return;
}
if (!isValidDate(day1, month1, year1) || !isValidDate(day2, month2, year2)) {
System.out.println("One or both dates are invalid.");
return;
}
int nthDay1 = getNthDay(day1, month1, year1);
int nthDay2 = getNthDay(day2, month2, year2);
int difference = Math.abs(nthDay1 - nthDay2);
System.out.println("The difference between the dates is " + difference + " days.");
}
}
Output :
Enter first date (DD/MM/YYYY): 12/05/2025
Enter second date (DD/MM/YYYY): 24/06/2025
The difference between the dates is 43 days.
5) Write a program to accept a date and value of n and display the date after n days.
import java.util.*;
public class DateAfterNDays {
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static boolean isValidDate(int day, int month, int year) {
if (month < 1 || month > 12 || day < 1 || year < 1) {
return false;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
daysInMonth[1] = 29;
}
return day <= daysInMonth[month - 1];
}
public static void getDateAfterNDays(int day, int month, int year, int n) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
while (n > 0) {
if (day + n > daysInMonth[month - 1]) {
n -= (daysInMonth[month - 1] - day + 1);
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
if (isLeapYear(year)) {
daysInMonth[1] = 29;
} else {
daysInMonth[1] = 28;
}
}
} else {
day += n;
n = 0;
}
}
String newDate = (day < 10 ? "0" + day : "" + day) + "/" +
(month < 10 ? "0" + month : "" + month) + "/" + year;
System.out.println("The date after given days: " + newDate);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter date (DD/MM/YYYY): ");
String date = scanner.nextLine().trim();
System.out.print("Enter number of days to add: ");
int n = scanner.nextInt();
String[] parts = date.split("/");
if (parts.length != 3) {
System.out.println("Invalid format! Use DD/MM/YYYY.");
return;
}
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (!isValidDate(day, month, year)) {
System.out.println("Invalid date entered.");
return;
}
getDateAfterNDays(day, month, year, n);
}
}
Output :
Enter date (DD/MM/YYYY): 12/05/2025
Enter number of days to add: 20
The date after given days: 01/06/2025
6) Write a program in java to accept a date and print the Name of the day if the day name of the 1 st Jan of the year
is given as input.
import java.util.*;
public class Q6
{
public static boolean isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static boolean isValidDate(int day, int month, int year)
{
if (month < 1 || month > 12 || day < 1 || year < 1) {
return false;
}
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
daysInMonth[1] = 29;
}
return day <= daysInMonth[month - 1];
}
public static int getNthDay(int day, int month, int year)
{
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
{
daysInMonth[1] = 29;
}
int nthDay = 0;
for (int i = 0; i < month - 1; i++)
{
nthDay += daysInMonth[i];
}
nthDay += day;
return nthDay;
}
public static String getDayName(String startDay, int nthDay)
{
String[] weekDays = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int startIndex = -1;
for (int i = 0; i < weekDays.length; i++)
{
if (weekDays[i].equalsIgnoreCase(startDay))
{
startIndex = i;
break;
}
}
if (startIndex == -1)
{
return "Invalid start day!";
}
return weekDays[(startIndex + (nthDay - 1)) % 7];
}
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter date (DD/MM/YYYY): ");
String date = scanner.nextLine().trim();
System.out.print("Enter the name of the day for 1st Jan of that year: ");
String startDay = scanner.nextLine().trim();
String[] parts = date.split("/");
if (parts.length != 3)
{
System.out.println("Invalid format! Use DD/MM/YYYY.");
return;
}
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
if (!isValidDate(day, month, year))
{
System.out.println("Invalid date entered.");
return;
}
int nthDay = getNthDay(day, month, year);
String dayName = getDayName(startDay, nthDay);
System.out.println("The day on " + date + " is: " + dayName);
}
}
Output :
Enter date (DD/MM/YYYY): 12/05/2025
Enter the name of the day for 1st Jan of that year: wednesday
The day on 12/05/2025 is: Monday