展开全部
请将每62616964757a686964616fe58685e5aeb9313332343236344个````换成Tab再查看源代码
// MyDate.java
//package cn.plause.test;
/**
* @author plause.cn
*/
public class MyDate {
````private int year = 1970;
````private int month = 0;
````private int date = 1;
````private String[] monthOfYear = new String[] {
````````````"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
````private int[] daysInMonth = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
````/**
```` * Creates a new instance of MyDate using specified year, month and date.
```` */
````public MyDate(int year, int month, int date) {
````````this.year = year;
````````this.month = month - 1;
````````this.date = date;
````````
````````if (! isValidDate()) {
````````````this.year = 1970;
````````````this.month = 0;
````````````this.date = 1;
````````}
````}
````/**
```` * Creates a copy of a created instance of MyDate.
```` */
````public MyDate(MyDate myDate) {
````````this(myDate.year, myDate.month + 1, myDate.date);
````}
````/**
```` * Indicates whether another date is "equal to" this date.
```` */
````public boolean equal(MyDate anotherDate) {
````````if (anotherDate == null) {
````````````return false;
````````}
````````if (this == anotherDate) {
````````````return true;
````````}
````````
````````return this.year == anotherDate.year
````````````````&& this.month == anotherDate.month
````````````````&& this.date == anotherDate.date;
````}
````/**
```` * Points to the following day of the current date.
```` */
````public void incrementDay() {
````````MyDate nextDate = new MyDate(this);
````````int days = daysInMonth[month];
````````
````````if (isLeapYear() && monthOfYear[month].equals("Feb")) {
````````````days++;
````````}
````````if (nextDate.date == days) {
````````````nextDate.date = 1;
````````````nextDate.month++;
````````````if (nextDate.month == monthOfYear.length) {
````````````````nextDate.month = 0;
````````````````nextDate.year++;
````````````}
````````} else {
````````````nextDate.date++;
````````}
````````this.year = nextDate.year;
````````this.month = nextDate.month;
````````this.date = nextDate.date;
````}
````public int getYear() {
````````return year;
````}
````public int getMonth() {
````````return month + 1;
````}
````public int getDate() {
````````return date;
````}
````/**
```` * Returns a string representation of the current date.
```` */
````public String toString() {
````````// return String.format("%d-%d-%d", year, month + 1, date);
````````return (new StringBuilder())
````````````````.append(year)
````````````````.append("-")
````````````````.append(month + 1)
````````````````.append("-")
````````````````.append(date)
````````````````.toString();
````}
````/**
```` * Indicates whether the current date is valid.
```` */
````private boolean isValidDate() {
````````if (year < 0) {
````````````return false;
````````}
````````if (! (month >= 0 && month < monthOfYear.length)) {
````````````return false;
````````}
````````if (! (date >= 1 && date <= 31)) {
````````````return false;
````````}
````````int days = daysInMonth[month];
````````if (! monthOfYear[month].equals("Feb")) {
````````````return date <= days;
````````}
````````return date <= days + (isLeapYear() ? 1 : 0);
````}
````/**
```` * Checks if the current year is a leap year.
```` * The second month(February) of a leap year has 29 not 28 days.
```` */
````private boolean isLeapYear() {
````````return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
````}
````/**
```` * Test
```` */
````public static void main(String[] args) {
````````MyDate date = new MyDate(2007, 10, 12);
````````for (int i = 0; i < 150; i++) {
````````````date.incrementDay();
````````````System.out.println(date);
````````}
````}
}
// MyDateTest.java
//package cn.plause.test;
/**
* @author plause.cn
*/
public class MyDateTest {
````public static void main(String[] args) {
````````MyDate now = new MyDate(2007, 10, 12);
````````MyDate next = new MyDate(2007, 10, 13);
````````System.out.println("NOW: ");
````````System.out.println("getYear: " + now.getYear());
````````System.out.println("getMonth: " + now.getMonth());
````````System.out.println("getDate: " + now.getDate());
````````System.out.println("toString: " + now);
````````System.out.println();
````````System.out.println("next day of NOW: ");
````````now.incrementDay();
````````System.out.println("now.incrmentDay is: " + now);
````````System.out.println();
````````System.out.println("NEXT: " + next);
````````System.out.println("NOW equals NEXT ?: " + now.equal(next));
````````MyDate monthTest = new MyDate(2007, 9, 30);
````````monthTest.incrementDay();
````````System.out.println("MONTH TEST:");
````````System.out.println("the following date of 2007-9-30 is " + monthTest);
````````MyDate yearTest = new MyDate(2007, 12, 31);
````````yearTest.incrementDay();
````````System.out.println("YEAR TEST:");
````````System.out.println("the following date of 2007-12-31 is " + yearTest);
````````MyDate leapTest1 = new MyDate(2007, 2, 28);
````````leapTest1.incrementDay();
````````System.out.println("LEAP TEST1:");
````````System.out.println("the following date of 2007-2-28 is " + leapTest1);
````````MyDate leapTest2 = new MyDate(2008, 2, 28);
````````leapTest2.incrementDay();
````````System.out.println("LEAP TEST2:");
````````System.out.println("the following date of 2008-2-28 is " + leapTest2);
````}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起