In java, the class Date was designed and implemented to keep track of a date, bu
ID: 3736841 • Letter: I
Question
In java, the class Date was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class Date so that, in addition to the operation already defned, it can perform the following operations on a date: set a month, set a day, set a year, return a month, return a day, return a year, test whether the year is a leap year, return the number of days in a month. Return the number of days passed in the year, Return the number of days remaining in the year, Calculate the new date by adding a fixed number of days to the date, Return a reference to the object containing a copy of the date, make a copy of another date, and write the definition of the methods to implement the operations defined for the class Date. //I need this in JAVA
Explanation / Answer
CODE:
public class DateExtension extends java.util.Date
{
//intilasing firstday,lasyday to find out days remaining year
val now = LocalDate.now() # 2016-02-19
val firstDay = now.`with`(firstDayOfYear()) # 2016-01-01
val lastDay = now.`with`(lastDayOfYear()) # 2016-12-31
// setter methods for month,day and year
public void setMonth( int m )
{
month= m;
}
public void setDay( int d )
{
day=d;
}
public void setYear( int y )
{
year=y;
}
// gettre methods for month,day and year
public int getMonth( )
{
return month;
}
public int getDay( )
{
return day;
}
public int getYear( )
{
return year;
}
//leapyear method
public void leapYear(int year)
{
boolean isLeapYear = false;
if(year % 400 == 0)
{
isLeapYear = true;
}
else if (year % 100 == 0)
{
isLeapYear = false;
}
else if(year % 4 == 0)
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
//Output the test result
if(isLeapYear)
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
public int daysInMonth(GregorianCalendar c)
{
int [] daysInMonths = {31,28,31,30,31,30,31,31,30,31,30,31};
daysInMonths[1] += c.isLeapYear(c.get(GregorianCalendar.YEAR)) ? 1 : 0;
return daysInMonths[c.get(GregorianCalendar.MONTH)];
}
public int daysPassed(date2)
{
return int days = Days.daysBetween(now,firstday).getDays();
}
public int daysRemaining(date)
{
return int days = Days.daysBetween(lastdate,now).getDays();
}
public newdate(int n)
{
c.add(Calendar.DATE, n);
Date dt = new Date();
System.out.print(dt.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.