Date Programs in Java
Date Programs in Java
1) Description:
Design a class Convert to find the date and the month from a given day number for a
particular year.
Example: If day number is 64 and the year is 2020, then the corresponding date would be:
March 4, 2020 i.e. (31 + 29 + 4 = 64)
Specify the class Convert giving details of the constructor(), void accept(), void
day_to_date() and void display(). Define a main() function to create an object and call the
functions accordingly to enable the task.
Program Code:
import java.util.Scanner;
class Convert{
int n;
int d;
int m;
int y;
public Convert(){
n = 0;
d = 0;
m = 1;
y = 0;
}
public void accept(){
Scanner in = new Scanner(System.in);
System.out.print("Day number: ");
n = in.nextInt();
System.out.print("Year: ");
y = in.nextInt();
}
public void day_to_date(){
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
day[2] = 29;
m = 1;
while(n > day[m]){
n -= day[m];
m++;
if(m == 12){
m = 1;
y++;
if(y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
day[2] = 29;
else
day[2] = 28;
}
}
d = n;
}
public void display(){
String month[] = {"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
System.out.println(month[m] + " " + d + ", " + y);
}
public static void main(String[] args){
Convert obj = new Convert();
obj.accept();
obj.day_to_date();
obj.display();
}
}
2) Description:
Write a program to input two valid dates, each comprising of Day (2 digits), Month (2 digits)
and Year (4 digits) and calculate the days elapsed between both the dates.
Test your program for the following data values:
(a)
FIRST DATE:
Day: 24
Month: 09
Year: 1960
SECOND DATE:
Day: 08
Month: 12
Year: 1852
Output: xxxxxxxx
(these are actual number of days elapsed)
(b)
FIRST DATE:
Day: 10
Month: 01
Year: 1952
SECOND DATE:
Day: 16
Month: 10
Year: 1952
Output: xxxxxxxx
(these are actual number of days elapsed)
Program Code:
import java.util.Scanner;
public class DaysBetween
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int d1[] = new int[3];
int d2[] = new int[3];
System.out.println("Enter First Date: ");
System.out.print("Enter Day: ");
d1[0] = in.nextInt();
System.out.print("Enter Month: ");
d1[1] = in.nextInt();
System.out.print("Enter Year: ");
d1[2] = in.nextInt();
System.out.println("Enter Second Date: ");
System.out.print("Enter Day: ");
d2[0] = in.nextInt();
System.out.print("Enter Month: ");
d2[1] = in.nextInt();
System.out.print("Enter Year: ");
d2[2] = in.nextInt();
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n1 = d1[2] * 365 + d1[0];
int n2 = d2[2] * 365 + d2[0];
for (int i = 0; i < d1[1] - 1; i++) {
n1 += monthDays[i];
}
for (int i = 0; i < d2[1] - 1; i++) {
n2 += monthDays[i];
}
int y = d1[1] <= 2 ? d1[2] - 1 : d1[2];
n1 += y / 4 - y / 100 + y / 400;
y = d2[1] <= 2 ? d2[2] - 1 : d2[2];
n2 += y / 4 - y / 100 + y / 400;
int daysElapsed = Math.abs(n2 - n1);
System.out.println("No of days Elapsed = " + daysElapsed);
}
}
3) Description:
Write a program in Java to accept day number (between 1 and 366) and year (yyyy) from the
user and display the corresponding date. Also accept ‘N’ from the user where (1 ≤ N ≤ 100)
to compute and display the future date ‘N’ days after the given date.
Display error message if the value of the day number or ‘N’ are not within the limit. Day
number is calculated taking 1st January of the given year as 1.
Test your program with given set of data and some random data.
Example 1
INPUT:
DAY NUMBER: 50
YEAR: 2024
N = 25
OUTPUT:
ENTERED DATE: FEBRUARY 19, 2024
25 DAYS LATER: MARCH 15, 2024
Example 2
INPUT:
DAY NUMBER: 321
YEAR: 2024
N: 77
OUTPUT:
ENTERED DATE: NOVEMBER 16, 2024
77 DAYS LATER: FEBRUARY 1, 2025
Example 3
INPUT:
DAY NUMBER: 400
YEAR: 2024
N: 125
OUTPUT:
INCORRECT DAY NUMBER
INCORRECT VALUE OF ‘N’
Program Code:
import java.util.Scanner;
class LaterDate{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean validDayNum = true;
boolean validN = true;
System.out.print("DAY NUMBER: ");
int dayNum = Integer.parseInt(in.nextLine());
System.out.print("YEAR: ");
int year = Integer.parseInt(in.nextLine());
System.out.print("N: ");
int n = Integer.parseInt(in.nextLine());
if(dayNum < 1 || (dayNum > 365 && !isLeap(year))){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
else if(dayNum > 366 && isLeap(year)){
validDayNum = false;
System.out.println("INCORRECT DAY NUMBER");
}
if(n < 1 || n > 100){
validN = false;
System.out.println("INCORRECT VALUE OF 'N'");
}
if(!validDayNum || !validN)
return;
int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String month[] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY",
"JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER",
"DECEMBER"};
if(isLeap(year))
day[1] = 29;
int index = 0;
while(dayNum > day[index]){
dayNum -= day[index++];
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
System.out.println("ENTERED DATE: " + month[index] + " " + dayNum + ", " + year);
System.out.print(n + " DAYS LATER: ");
while(n > 0){
dayNum++;
n--;
if(dayNum > day[index]){
dayNum = 1;
index++;
if(index == 12){
index = 0;
year++;
if(isLeap(year))
day[1] = 29;
else
day[1] = 28;
}
}
}
System.out.println(month[index] + " " + dayNum + ", " + year);
}
public static boolean isLeap(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
return true;
return false;
}
}
4) Description:
Write a program to accept a date in the string format dd/mm/yyyy and accept the name of the
day on 1st of January of the corresponding year. Find the day for the given date.
Example:
Input:
Date: 5/7/2001
Day on 1st January : MONDAY
Output:
Day on 5/7/2001 : THURSDAY
The program should include the part for validating the inputs namely the date and day on 1st
January of that year.
Program Code:
import java.util.*;
public class DayName
{
public static void main(String args[]) {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String dayNames[] = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY"};
Scanner in = new Scanner(System.in);
System.out.print("Enter Date(dd/mm/yyyy): ");
String dateStr = in.nextLine();
if (y % 400 == 0) {
ret = true;
}
else if (y % 100 == 0) {
ret = false;
}
else if (y % 4 == 0) {
ret = true;
}
else {
ret = false;
}
return ret;
}
}
{
/* First Part */
System.out.println(“Date in dd/mm/yyyy format = “+dd+”/”+mm+”/”+yy);
/* Second Part */
System.out.print(“Date in dd, month name, yyyy format = “+dd+” “+month[m]+”, “+yy);
}
}
else
System.out.println(“Wrong Input”);
}
}
Output:
Enter any date in 8 digits (ddmmyyyy) format: 12012018
Date in dd/mm/yyyy format = 12/01/2018
Date in dd, month name, yyyy format = 12 January, 2018
5) Description:
Java program for generating the calendar of any desired year and month. Say the user wants
to get the calendar of April 2011. Then, he is required to enter the year along with the month
as integers and the output would return the desired month’s calendar for the respective year in
a proper format.
Program Code:
// Java Program to Generate Desired Calendar Without calendar.get() function or Inputting
// the Year and the Month
import java.io.*;
import java.util.Scanner;
public class Calen {
public static void main(String a[])
{
if (m > 12) {
m = 1;
y++;
}
if (dy == 7) {
dy = 0;
}
}
int c = dy;
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
ar[1] = 29;
}
else {
ar[1] = 28;
}
// Print the desired month of input year
System.out.println("MONTH:" + month[mm - 1]);
for (int k = 0; k < 7; k++) {
System.out.print(" " + day[k]);
}
System.out.println();
for (int j = 1; j <= (ar[mm - 1] + dy); j++) {
if (j > 6) {
dy = dy % 6;
}
}
int spaces = dy;
if (spaces < 0)
spaces = 6;
// Printing the calendar
for (int i = 0; i < spaces; i++)
System.out.print(" ");
for (int i = 1; i <= ar[mm - 1]; i++) {
System.out.printf(" %4d ", i);
if (((i + spaces) % 7 == 0)
|| (i == ar[mm - 1]))
System.out.println();
}
}
………………………….