java 定义 时间_用java定义一个日期类,急!!!

展开全部

请将每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);

````}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值