In: Computer Science
import chapter6.date.SimpleDate;
import java.util.Scanner;
public class SimpleDateTestDefault {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
SimpleDate d1 = new SimpleDate();
SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(),
stdin.nextInt());
System.out.println(d1);
System.out.println(d2);
System.out.println(d1.before(d2));
System.out.println(d2.before(d1));
}
}
Implement SimpleDate class in chapter6.date package with the following attributes:
The class should have the following methods:
Code to copy along with screenshots of code and
output are provided.
Please refer to screenshots to understand the indentation of
code.
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of "SimpleData.java":
Screenshots of "SimpleDateTestDefault.java"
Screenshots of Output :
Code to copy("SimpleDate.java"):
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class SimpleDate {
// integer variables
int day;
int month;
int year;
// constructor which takes three integer
arguments
SimpleDate(int day, int month, int year)
{
// initializing varaible according to arguments
this.day = day;
this.month = month;
this.year = year;
}
// constructor with no parameters
SimpleDate()
{
// initializing with default
values
this.day = 01;
this.month = 01;
this.year = 2000;
}
// method to return number of days from this to
another
int numberOfDays(SimpleDate another)
{
int n = 0;
// creating date strings of
SimpleDate
String dateString =
Integer.toString(this.month) + "/"+Integer.toString(this.day)+
"/"+Integer.toString(this.year);
String date2String = Integer.toString(another.month) +
"/"+Integer.toString(another.day)+
"/"+Integer.toString(another.year);
// format of date
String format = "MM/dd/yyyy";
// new SimpleDateFormat object
SimpleDateFormat sDateFormat = new
SimpleDateFormat(format);
try
{
// parsing
dateString as into Date
Date d1 =
sDateFormat.parse(dateString);
//parsing date2String as into
Date
Date d2 =
sDateFormat.parse(date2String);
// calculating difference of days using getTime function
n = (int)
(((d2.getTime()-d1.getTime())/(1000*60*60*24)));
}
catch (ParseException e)
{
// try catch to catch parsing
exception
}
// return number of days
return n;
}
// method to check if date is before another
date
boolean before(SimpleDate another)
{
boolean ret = false;
// if numberOfDays is positive then another date is
after this date
if(numberOfDays(another) > 0)
{
ret = true;
}
// return
return ret;
}
// method to convert this date to string
public String toString()
{
// returning string
return
Integer.toString(this.month)+"/"+Integer.toString(this.day)+"/"+Integer.toString(this.year);
}
}
Code to copy("SimpleDateTestDefault.java"):
import java.util.Scanner;
public class SimpleDateTestDefault {
public static void main(String args[])
{
Scanner stdin = new Scanner(System.in);
// creating SimpleDate instance using default
values
SimpleDate d1 = new SimpleDate();
System.out.print("Enter day month and year : ");
// creating SimpleDate instance using values entered
by user
SimpleDate d2 = new SimpleDate(stdin.nextInt(),
stdin.nextInt(), stdin.nextInt());
// converting date to strings
System.out.println("d1: "+d1.toString());
System.out.println("d2: "+d2.toString());
// checking before(SimpleDate) function
System.out.println("Is d1 before d2?:
"+d1.before(d2));
System.out.println("Is d2 before d1?:
"+d2.before(d1));
//checking numberOfDays(SimpleDate another)
function
System.out.print("Number of days between(d2 -
d1):"+d1.numberOfDays(d2));
}
}
==============XXXXXX==============================