栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java 定义 时间

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 定义 时间

展开全部

请将每62616964757a686964616fe58685e5aeb9313332343236344个````换成Tab再查看源代码

// MyDate.java

//package cn.plause.test;

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};

````

````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;

````````}

````}

````

````public MyDate(MyDate myDate) {

````````this(myDate.year, myDate.month + 1, myDate.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;

````}

````

````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;

````}

````

````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();

````}

````

````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);

````}

````

````private boolean isLeapYear() {

````````return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;

````}

````

````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;

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);

````}

}

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号