In: Computer Science
/////////////////JAVA PLEASE/////////////////////////////////
Create a class called GVdate to keep track of a calendar date including month, day and year. You can do simple things like checking if it is your birthday, advancing to the next day, checking if a given date is valid and checking if it is a leap year.
Class Fields/Instance Variables
Constructors
Accessor Methods
Mutator Methods
Please find the below solution for this problem:-
package qvdate;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class GVdate {
private int calDate;
private int calMonth;
private int calYear;
private final int birthday=26;
private final int birthMonth=12;
public GVdate() {
this.calDate=12;
this.calMonth=10;
this.calYear=2020;
}
public GVdate (int m, int d, int y) {
this.calDate=d;
this.calMonth=m;
this.calYear=y;
}
public String toString() {
return
calMonth+"/"+calDate+"/"+calYear;
}
public boolean isMyBirthday() {
if(calMonth==birthMonth
&&calDate==birthday) {
return
true;
}
return false;
}
public void setDate (int m, int d, int y) {
this.calDate=d;
this.calMonth=m;
this.calYear=y;
}
public boolean isDateValid() {
String dateStr=toString();
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
return false;
}
return true;
}
public boolean isLeapYear() {
boolean leapYear = false;
if(calYear % 4 == 0)
{
if( calYear % 100 == 0)
{
if ( calYear % 400 == 0)
leapYear = true;
else
leapYear = false;
}
else
leapYear = true;
}
else
leapYear = false;
return leapYear;
}
public Date advancingtonextday() {
Calendar c =
Calendar.getInstance();
DateFormat sdf = new
SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date currentDate = null;
try {
currentDate=sdf.parse(toString());
} catch (ParseException e) {
}
c.setTime(currentDate);
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
return newDate;
}
public int getCalDate() {
return calDate;
}
public void setCalDate(int calDate) {
this.calDate = calDate;
}
public int getCalMonth() {
return calMonth;
}
public void setCalMonth(int calMonth) {
this.calMonth = calMonth;
}
public int getCalYear() {
return calYear;
}
public void setCalYear(int calYear) {
this.calYear = calYear;
}
}