📄 date.java
字号:
package Osbert;
public class Date
{
public static Date currentDate;
//initialize to 1/1/1900
Date() { year = 1900; month = 1; day = 1; }
// private Date fields
private int year; // year of date
private int month; // month of date
private int day; // day of date
// getter-setters for Date
public int getYear() { return year; }
public int getMonth() { return month; }
public int getDay() { return day; }
public void setYear(int y) { year = y; }
public void setMonth(int m) { month = m; }
public void setDay(int d) { day = d; }
// Date methods
void subtractOneYear () { year--; }
// define the i/o stream operator for date
public String toString()
{
String ret ="";
if(month < 10)
ret = "0";
ret = ret + month + "/";
if(day < 10)
ret = ret + "0";
ret = ret + day + "/" + year;
return ret;
}
// -------------------------------------------------------------------------------------------------------------------
public boolean parseDate (String dateStr)
//
// given a valid date in the format MM/DD/YYYY, parses the
// respective parts and returns whether the date is valid
//
{
String temp = new String(); // for storing parts of date
//
// retrieve the year component from the date string
//
temp = dateStr.substring(6, 10);
year = Integer.parseInt(temp);
//
// retrieve the day component from the date string
//
temp = dateStr.substring(3, 5);
day = Integer.parseInt(temp);
//
// retrieve the month component from the date string
//
temp = dateStr.substring(0, 2);
month = Integer.parseInt(temp);
return validDate(); // return whether the parsed date is valid
} // parseDate
//-------------------------------------------------------------------------------------------------------------------
public int compare (Date aDate)
//
// determines the temporal order of two dates
// returns -1 if this < aDate
// returns 0 if the dates are the same
// returns 1 if aDate < this
//
{
//
// first, compare the respective years
//
if (year < aDate.getYear())
return -1;
if (year > aDate.getYear())
return 1;
//
// next, compare the months
//
if (month < aDate.getMonth())
return -1;
if (month > aDate.getMonth())
return 1;
//
// finally, compare the respective days
//
if (day < aDate.getDay())
return -1;
if (day > aDate.getDay())
return 1;
else
return 0;
} // compare
//-------------------------------------------------------------------------------------------------------------------
private boolean validDate ()
//
// determines if the date corresponds to a valid date
// of the form mm/dd/yyyy, where 1 <= mm <= 12, 1 <= dd <= 31, 1900 <= yyyy <= 2010
// return true iff the date is valid
//
{
//
// check that the year component makes sense
//
if ((year < 1900) || (year > 2010))
return false;
//
// check that the month component makes sense
//
if ((month < 1) || (month > 12))
return false;
//
// check that the day component makes sense
//
if ((day < 1) || (day > 31))
return false;
else
return true;
} // validDate
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -