public class Date {
private int month; // instance variable for value of the date’s month
private int day; // instance variable for value of the date’s day
private int year; // instance variable for the value of the dates
private int time; // instance variable for the value of the date's time
// Default constructor: set the instance variables to default values: month = 1; day = 1; year = 1900;
public Date() {
month = 1;
day = 1;
year = 1900;
}
// Constructor to set the date
// The instance variables month, day, and year are set according to received parameters.
public Date(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
// Method to set the date
// The instance variables month, day, and year are set according to received parameters.
public void setDate(int month, int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}
// Method to return the date’s month value
public int getMonth() {
return month;
}
// Method to return the date’s day value
public int getDay() {
return day;
}
// Method to return the date's time value
public double getTime()
{
return time;
}
// Method to return the date’s year value
public int getYear() {
return year;
}
// Override of inherited toString() method to return the date in the form of mm-dd-yyyy
public String toString() {
return (month + "-" + day + "-" + year);
}
}
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ExtendedDate extends Date
{
Calendar calendar = new GregorianCalendar();
private static final int[] daysInMonth = { 0, 31, 29, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // Jan = 0, not 1
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);
int hour = calendar.get(Calendar.HOUR); // 12 hour clock
int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);
public static void main(String args[])
{
Calendar calendar = new GregorianCalendar();
Calendar calendar2 = new GregorianCalendar();
calendar2.set(Calendar.DAY_OF_MONTH, 8);
System.out.println("Cal1 YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("Cal1 MONTH:" + calendar.get(Calendar.MONTH));
System.out.println("Cal1 DAY: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("Cal2 YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("Cal2 MONTH:" + calendar.get(Calendar.MONTH));
System.out.println("Cal2 DAY: " + calendar.get(Calendar.DAY_OF_MONTH));
}
public ExtendedDate() {
super();
}
//Constructor that accepts parameters (2)
public ExtendedDate(int month, int day, int year) {
super(month, day, year);
}
//Method to set the date – override inherited setDate method
//If data is invalid, do not change the receiver (2)
public void setDate(int monthInt, int dayInt, int yearInt)
{
setDate(getMonth(), getDay(), getYear());
}
//Method to set month ensuring that only valid changes are made
public void setMonth(int anInt)
{
getMonth();
}
//Method to set day ensuring that only valid changes are made
public void setDay(int anInt)
{
getDay();
}
//Method to set year ensuring that only valid changes are made
{
getYear();
}
//New method to verify a date
//Valid range is 1-1-1900 through 12-31-2999
public static boolean isGodDate(int month, int day, int year)
{
if (1900 < year || year < 2999)
return false;
if (month < 1 || month > 12 )
return false;
if (day < 1 || day > daysInMonth[month])
return false;
if (month == 2 && day == 29 && !isLeapYear(year))
return false;
return true;
}
//check for a leap year
public static boolean isLeapYear(int aYear)
{
if (aYear % 400 == 0)
{
return true;
}
else if (aYear % 100 == 0)
{
return false;
}
else if (aYear % 4 == 0)
{
return true;
}
else
return false;
}
//Instance method version of the above (7g)
public boolean isLeapYear()
{
return ExtendedDate.isLeapYear(getYear());
}
//Return number of days in a month
public int daysInMonth()
{
if (getMonth() >= 1 && getMonth() <= 12)
if (getMonth() != 2)
return daysInMonth[getMonth()];
else if (isLeapYear(getYear()))
return 29;
else
return 28;
else
return 0;
}
public int daysToDate()
{
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(weekOfYear, month, dayOfMonth);
end.set(weekOfYear, month, dayOfMonth);
java.util.Date startDate = start.getTime();
java.util.Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormat.format(startDate)+" and "+
dateFormat.format(endDate)+" is "+
diffDays+" days.");
return dayOfMonth;
}
//Return number of days remaining in year (7j)
public int daysRemainingInYear()
{
Calendar c1 = Calendar.getInstance();
int doy1 = c1.get(Calendar.DAY_OF_YEAR);
int year = c1.get(Calendar.YEAR);
Calendar calendar2 = new GregorianCalendar(month,getDay(), year);
int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);
int days = doy2 = doy1;
System.out.println(days + " days remain in current year");
return days;
}
//Create a new GoodDate object with the values that result from adding a number of days to an existing GoodDate object. Cannot result in an invalid GoodDate being created. If an invalid date would be created, return a GoodDate with default values. (7k)
public ExtendedDate futureDate(int Day)
{
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(weekOfYear, month, dayOfMonth);
end.set(weekOfYear, month, dayOfMonth);
java.util.Date startDate = start.getTime();
java.util.Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println("The difference between "+
dateFormat.format(startDate)+" and "+
dateFormat.format(endDate)+" is "+
diffDays+" days.");
return new ExtendedDate();
}
}
*** EDIT ***
Please use a more descriptive title than "Problem"
This post has been edited by GunnerInc: 09 June 2012 - 05:19 PM
Reason for edit:: Added code tags and a more descriptive title

New Topic/Question
Reply



MultiQuote




|