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);
````}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
java 定义 时间_用java定义一个日期类,急!!!相关推荐
- java纪元时间_关于java:如何将时间戳字符串转换为纪元时间?
我有格式2017-18-08 11:45:30.345的时间戳. 我想将它转换为纪元时间,所以我在下面做: String timeDateStr ="2017-18-08 11:45:30. ...
- java定义一个日期类 包括年 月 日_定义一个日期类:包括年、月、日三个成员变量,显示日期的方法...
/*定义一个日期类:包括年.月.日三个成员变量,显示日期的方法 * 提供构造方法:定义无参构造方法,和有参构造方法 */ 代码如下: public class Demo { public static ...
- java定义一个日期类 包括年 月 日_【说明】 设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期..._考试资料网...
填空题[说明] 设计一个日期类Date包括年.月.日等私有数据成员.要求实现日期的基本运算,如某日期加上天数.某日期减去天数.两日期相差的天数等. 在Date类中设计如下重载运算符函数: Date o ...
- java编写salary函数_编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量...
编写一个Java程序,在程序中包含一个Employee类,Employee类包含name.age.salary三个成员变量,Employee类中有4个构造方法,分别为无参的.带一个参数用来对name属 ...
- 编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量
编写一个Java程序,在程序中包含一个Employee类,Employee类包含name.age.salary三个成员变量,Employee类中有4个构造方法,分别为无参的.带一个参数用来对name属 ...
- python设计一个date类数据成员有年月日_设计一个日期类Date,包括年、月、日等私有成员。要求实现日期的基本运算,例如某日期加上天数或减去天数...
/*设计一个日期类Date,包括年.月.日等私有成员.要求实现日期的基本运算,例如某日期加上天数或减去天数 ,两日期相减的天数等. 实现要求: 实现运算符加与减的重载 设计一个日期类Date,包括年. ...
- 常类型的使用 常成员函数(设计一个日期类和时间)
普通成员函数可以访问常数据成员但是不能改变常数据成员的值: 普通成员函数不可以访问常对象的数据成员且不能改变常对象的数据成员的值: 常成员函数可以访问普通数据成员但是不可以改变普通数据成员的值: 常成 ...
- 设计一个日期类Date
//用C++++设计一个日期类Date,该类用于表示日期值(年.月.日). //要求除了能够通过相应的成员函数设置和获取日期值外,还能够实现将日期加一天的操作. #include<iostrea ...
- (C++)设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,包括某日期加上指定天数、某日期减去指定天数、两个日期相差的天数等。
C++面向对象程序设计课后作业第239页第5题 题目要求:设计一个日期类Date,包括年.月.日等私有数据成员.要求实现日期的基本运算,包括某日期加上指定天数.某日期减去指定天数.两个日期相差的天数等 ...
最新文章
- Python机器学习——Agglomerative层次聚类
- tensorflow sobel算子实现
- FPGA学习之路—应用程序—基于Verilog设计单总线8位ALU
- 八位流水灯的verilog代码_Arduino入门 第七节-彗星灯 呼吸流水灯
- Atitit数据库层次架构表与知识点 attilax 总结
- 保证速度与心情——pdg转pdf与djvu转pdf大法(不像网上的好多方法那样麻烦,方便快捷,纯傻瓜化操作!)
- Sentaurus入门(1):工艺仿真
- 有哪些让你印象深刻的bug?
- springMVC+mybatis
- 生成yolov5.wts文件出错
- 浏览器使用flash时出现此Flash Player 与您所在地区不相容的提示解决方法
- Date()常用方法getMonth, getFullYear等
- Zabbix实现短信报警
- 深拷⻉和浅拷⻉区别是什么?
- 青岛小学 初中有计算机编程比赛,青岛市电脑制作活动 程序设计竞赛 一等奖...
- 基于PHP+MySQL的大学生求职招聘网站
- 新版 CAD 2017 阵列怎么控制角度
- SQL之HAVING
- 点击按钮触发声音(xaml实现)
- 如何人工给电脑加速?(经典知识)