用面向对象和面向过程思想解决两个日期间隔的天数和两个时间之间间隔秒数以及两个时间的间隔
目录
- 两个日期间隔的天数
- 类的实现
- 类的源码:
- 具体调用:
- 具体结果:
- 两个时间之间间隔秒数
- 类的定义:
- 类的调用
- 具体结果
- 两个日期和时间之间间隔时间
- 类的定义:
两个日期间隔的天数
类的实现
求解两个日期间,间隔的天数,我们可以利用面向对象的思想,将日期抽象出3个属性:year,month,day。
public class MyDate {
public int year;
public int month;
public int day;
}
在类中,建立MyDate的构造函数,
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
通过在主函数内部建两个对象,将两个日期定义出来。同时直接传参
MyDate from = new MyDate(2020, 1, 1);
MyDate to = new MyDate(2020, 1, 3);
对于传入的参数,我们要进行合法性判断:
if (year < 1900 || year > 3000) {
throw new RuntimeException("year参数有问题!");
}
if (month < 1 || month > 12) {
throw new RuntimeException("month参数有问题!");
}
if (day < 1 || day > getMonthDay(year, month)) {
throw new RuntimeException("day参数有问题!");
}
同时还要判断是否闰年,以及月份的日期是多少天:
private int getMonthDay(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
// 利用不加 break,代码会继续向下的规则
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return -1;
}
}
//是否是闰年
private boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
将起始日期传递进来:
//将from的参数传进来
public MyDate(MyDate from) {
this.year = from.year;
this.month = from.month;
this.day = from.day;
}
创建方法,计算相差的天数:
利用循环,只要传进来的日期和现在日期不相等,count++;
//计算相差天数:
public int method(MyDate from) {
if (this.compareTo(from) <= 0) {
throw new RuntimeException("from 的日期必须当前日期之前");
}
MyDate fromCopy = new MyDate(from);
int count = 0;
while (fromCopy.compareTo(this) < 0) {
System.out.println(fromCopy);
fromCopy.increment();
count++;
}
return count;
//return count = between(this) - between(fromCopy);
}
同时我们也要考虑,进位的问题:
//考虑进位
private void increment() {
day++;
if (day <= getMonthDay(year, month)) {
// day 不需要考虑进位
return;
}
// day 需要考虑日期进位
month++;
day = 1;
if (month <= 12) {
// month 不需要考虑进位
return;
}
year++;
month = 1;
}
}
比较两个日期问题
private int compareTo(MyDate from) {
if (year != from.year) {
return year - from.year;
}
if (month != from.month) {
return month - from.month;
}
return day - from.day;
}
类的源码:
/** * 相差多少天的计算 */
public class MyDate {
public int year;
public int month;
public int day;
//判断合法性
public MyDate(int year, int month, int day) {
if (year < 1900 || year > 3000) {
throw new RuntimeException("year参数有问题!");
}
if (month < 1 || month > 12) {
throw new RuntimeException("month参数有问题!");
}
if (day < 1 || day > getMonthDay(year, month)) {
throw new RuntimeException("day参数有问题!");
}
this.year = year;
this.month = month;
this.day = day;
}
private int getMonthDay(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
// 利用不加 break,代码会继续向下的规则
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return -1;
}
}
//是否是闰年
private boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
//将from的参数传进来
public MyDate(MyDate from) {
this.year = from.year;
this.month = from.month;
this.day = from.day;
}
@Override
public String toString() {
return "MyDate{" + "year=" + year + ", month=" + month + ", day=" + day + '}';
}
//计算相差天数:
public int method(MyDate from) {
if (this.compareTo(from) <= 0) {
throw new RuntimeException("from 的日期必须当前日期之前");
}
MyDate fromCopy = new MyDate(from);
int count = 0;
while (fromCopy.compareTo(this) < 0) {
System.out.println(fromCopy);
fromCopy.increment();
count++;
}
return count;
//return count = between(this) - between(fromCopy);
}
//考虑进位
private void increment() {
day++;
if (day <= getMonthDay(year, month)) {
// day 不需要考虑进位
return;
}
// day 需要考虑日期进位
month++;
day = 1;
if (month <= 12) {
// month 不需要考虑进位
return;
}
year++;
month = 1;
}
private int compareTo(MyDate from) {
if (year != from.year) {
return year - from.year;
}
if (month != from.month) {
return month - from.month;
}
return day - from.day;
}
}
具体调用:
public class MyDateTest {
public static void main(String[] args) {
MyDate from = new MyDate(2020, 1, 1);
MyDate to = new MyDate(2020, 1, 3);
System.out.println(to.method(from));
System.out.printf("从 %s 到 %s 经过了 %d 天\n",from, to, to.method(from));
}
}
具体结果:
两个时间之间间隔秒数
这个题目和上面的题目思想以及用法都十分的类似,都是这样考虑的问题,
在这里我就只放源代码和结果了:
类的定义:
/** *计算两个时间差 */
public class Mytime {
public int hour;
public int min;
public int sec;
public Mytime(int hour, int min, int sec) {
if (hour<0||hour>=24){
throw new RuntimeException("hour参数不合法");
}
if (min<0||min>=60){
throw new RuntimeException("min参数不合法");
}
if (sec<0||sec>=60){
throw new RuntimeException("sec参数不合法");
}
this.hour = hour;
this.min = min;
this.sec = sec;
}
@Override
public String toString() {
return "Mytime{" + "hour=" + hour + ", min=" + min + ", sec=" + sec + '}';
}
public Mytime(Mytime fromTime){
this.hour = fromTime.hour;
this.min = fromTime.min;
this.sec = fromTime.sec;
}
public int methodTime(Mytime fromTime){
if (this.compareToTime(fromTime) <= 0) {
throw new RuntimeException("from 的时间必须当前时间之前");
}
Mytime fromCopy = new Mytime(fromTime);
int count = 0;
while (fromCopy.compareToTime(this) < 0) {
System.out.println(fromCopy);
fromCopy.increment();
count++;
}
return count;
}
//考虑进位
private void increment() {
sec++;
if (sec < 60) {
return;
}
min++;
sec = 0;
if (min < 60) {
return;
}
hour++;
min =0 ;
}
private int compareToTime(Mytime fromTime) {
if (hour != fromTime.hour) {
return hour - fromTime.hour;
}
if (min != fromTime.min) {
return min - fromTime.min;
}
return sec - fromTime.sec;
}
}
类的调用
public class MytimeTest {
public static void main(String[] args) {
Mytime fromTime=new Mytime(9,10,23);
Mytime toTime=new Mytime(10,10,38);
System.out.printf("从 %s 到 %s 经过了 %d 秒\n",fromTime, toTime, toTime.methodTime(fromTime));
}
}
具体结果
两个日期和时间之间间隔时间
类的定义:
package DateTime;
/** *计算日期 */
public class MyDate {
private int year;
private int mon;
private int day;
public MyDate(MyDate Date) {
this.year = Date.year;
this.mon = Date.mon;
this.day = Date.day;
}
public MyDate(int year, int mon, int day) {
//判断参数的合法性
cheak(year, mon, day);
this.year = year;
this.mon = mon;
this.day = day;
}
@Override
public String toString() {
return String.format("%d-%02d-%02d", year, mon, day);
}
private void cheak(int year, int mon, int day) {
//校验参数的合法性
if (mon < 1 || mon > 12) {
throw new RuntimeException("mon的范围是1-12");
}
int days = getMonYear(year, mon);
if (day < 1 || day > days) {
throw new RuntimeException("day的范围是1-" + days);
}
}
private static int getMonYear(int year, int mon) {
if (mon == 2 && IsleapYear(year)) {
return 29;
}
return DAY[mon - 1];
}
private static boolean IsleapYear(int year) {
return (year % 4 == 0 && year % 400 != 0) || (year % 400 == 0);
}
private static final int[] DAY = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//往后走一天
public void next() {
day++;
if (day <= getMonYear(year, mon)) {
return;
}
mon++;
day = 1;
if (mon <= 12) {
return;
}
year++;
mon = 1;
}
//往前走一天
public void previous() {
day--;
if (day > 0) {
return;
}
mon--;
if (mon > 0) {
day = getMonYear(year, mon);
return;
}
year--;
mon = 12;
day = getMonYear(year, mon);
}
}
package DateTime;
/** * 计算时间 */
public class MyTime {
private int hour;
private int min;
private int sec;
public MyTime(MyTime time) {
this.hour = time.hour;
this.min = time.min;
this.sec = time.sec;
}
public MyTime(int hour, int min, int sec) {
cheak(hour, min, sec);
this.hour = hour;
this.min = min;
this.sec = sec;
}
//往后走1秒
public boolean next() {
sec++;
if (sec < 60) {
return false;
}
min++;
sec = 0;
if (min < 60) {
return false;
}
hour++;
min = 0;
if (hour < 24) {
return false;
}
hour = 0;
return true;
}
//往前走1秒
public boolean previous() {
sec--;
if (sec >= 0) {
return false;
}
min--;
sec = 59;
if (min >= 0) {
return false;
}
hour--;
min = 59;
if (hour >= 0) {
return false;
}
hour = 23;
return true;
}
@Override
public String toString() {
return String.format("%02d:%02d:%02d", hour, min, sec);
}
//合法性校验
private static void cheak(int hour, int min, int sec) {
if (hour < 0 || hour >= 24) {
throw new RuntimeException("hour参数有问题");
}
if (min < 0 || min >= 60) {
throw new RuntimeException("min参数有问题");
}
if (sec < 0 || sec >= 60) {
throw new RuntimeException("sec参数有问题");
}
}
}
package DateTime;
/** * 计算时间日期 */
public class DateTime {
private MyDate date;
private MyTime time;
public DateTime(DateTime datetime) {
this.date = new MyDate(datetime.date);
this.time = new MyTime(0, 0, 0);
}
public DateTime(int year, int month, int day, int hour, int minute, int second) {
this.date = new MyDate(year, month, day);
this.time = new MyTime(hour, minute, second);
}
public DateTime(int year, int month, int day) {
this(year, month, day, 0, 0, 0);
}
public DateTime(MyDate date) {
this.date = new MyDate(date);
}
// 向后走一秒
public void next() {
if (time.next()) {
date.next();
}
}
// 向前走一秒
public void previous() {
if (time.previous()) {
date.previous();
}
}
@Override
public String toString() {
return String.format("%s %s", date, time);
}
}
package DateTime;
import java.util.concurrent.TimeUnit;
public class Teat {
public static void main(String[] args) throws InterruptedException {
DateTime datetime = new DateTime(2021, 12, 31, 23, 59, 53);
while (true) {
System.out.println(datetime);
datetime.next();
TimeUnit.SECONDS.sleep(1); // 让代码停顿 1 秒
}
}
}
还没有评论,来说两句吧...