用面向对象和面向过程思想解决两个日期间隔的天数和两个时间之间间隔秒数以及两个时间的间隔

蔚落 2022-11-19 09:43 255阅读 0赞

目录

  • 两个日期间隔的天数
    • 类的实现
    • 类的源码:
    • 具体调用:
    • 具体结果:
  • 两个时间之间间隔秒数
    • 类的定义:
    • 类的调用
    • 具体结果
  • 两个日期和时间之间间隔时间
    • 类的定义:

两个日期间隔的天数

类的实现

求解两个日期间,间隔的天数,我们可以利用面向对象的思想,将日期抽象出3个属性:year,month,day。

  1. public class MyDate {
  2. public int year;
  3. public int month;
  4. public int day;
  5. }

在类中,建立MyDate的构造函数,

  1. public MyDate(int year, int month, int day) {
  2. this.year = year;
  3. this.month = month;
  4. this.day = day;
  5. }

通过在主函数内部建两个对象,将两个日期定义出来。同时直接传参

  1. MyDate from = new MyDate(2020, 1, 1);
  2. MyDate to = new MyDate(2020, 1, 3);

对于传入的参数,我们要进行合法性判断:

  1. if (year < 1900 || year > 3000) {
  2. throw new RuntimeException("year参数有问题!");
  3. }
  4. if (month < 1 || month > 12) {
  5. throw new RuntimeException("month参数有问题!");
  6. }
  7. if (day < 1 || day > getMonthDay(year, month)) {
  8. throw new RuntimeException("day参数有问题!");
  9. }

同时还要判断是否闰年,以及月份的日期是多少天:

  1. private int getMonthDay(int year, int month) {
  2. switch (month) {
  3. case 1:
  4. case 3:
  5. case 5:
  6. case 7:
  7. case 8:
  8. case 10:
  9. case 12:
  10. // 利用不加 break,代码会继续向下的规则
  11. return 31;
  12. case 4:
  13. case 6:
  14. case 9:
  15. case 11:
  16. return 30;
  17. case 2:
  18. return isLeapYear(year) ? 29 : 28;
  19. default:
  20. return -1;
  21. }
  22. }
  23. //是否是闰年
  24. private boolean isLeapYear(int year) {
  25. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  26. }

将起始日期传递进来:

  1. //将from的参数传进来
  2. public MyDate(MyDate from) {
  3. this.year = from.year;
  4. this.month = from.month;
  5. this.day = from.day;
  6. }

创建方法,计算相差的天数:
利用循环,只要传进来的日期和现在日期不相等,count++;

  1. //计算相差天数:
  2. public int method(MyDate from) {
  3. if (this.compareTo(from) <= 0) {
  4. throw new RuntimeException("from 的日期必须当前日期之前");
  5. }
  6. MyDate fromCopy = new MyDate(from);
  7. int count = 0;
  8. while (fromCopy.compareTo(this) < 0) {
  9. System.out.println(fromCopy);
  10. fromCopy.increment();
  11. count++;
  12. }
  13. return count;
  14. //return count = between(this) - between(fromCopy);
  15. }

同时我们也要考虑,进位的问题:

  1. //考虑进位
  2. private void increment() {
  3. day++;
  4. if (day <= getMonthDay(year, month)) {
  5. // day 不需要考虑进位
  6. return;
  7. }
  8. // day 需要考虑日期进位
  9. month++;
  10. day = 1;
  11. if (month <= 12) {
  12. // month 不需要考虑进位
  13. return;
  14. }
  15. year++;
  16. month = 1;
  17. }
  18. }

比较两个日期问题

  1. private int compareTo(MyDate from) {
  2. if (year != from.year) {
  3. return year - from.year;
  4. }
  5. if (month != from.month) {
  6. return month - from.month;
  7. }
  8. return day - from.day;
  9. }

类的源码:

  1. /** * 相差多少天的计算 */
  2. public class MyDate {
  3. public int year;
  4. public int month;
  5. public int day;
  6. //判断合法性
  7. public MyDate(int year, int month, int day) {
  8. if (year < 1900 || year > 3000) {
  9. throw new RuntimeException("year参数有问题!");
  10. }
  11. if (month < 1 || month > 12) {
  12. throw new RuntimeException("month参数有问题!");
  13. }
  14. if (day < 1 || day > getMonthDay(year, month)) {
  15. throw new RuntimeException("day参数有问题!");
  16. }
  17. this.year = year;
  18. this.month = month;
  19. this.day = day;
  20. }
  21. private int getMonthDay(int year, int month) {
  22. switch (month) {
  23. case 1:
  24. case 3:
  25. case 5:
  26. case 7:
  27. case 8:
  28. case 10:
  29. case 12:
  30. // 利用不加 break,代码会继续向下的规则
  31. return 31;
  32. case 4:
  33. case 6:
  34. case 9:
  35. case 11:
  36. return 30;
  37. case 2:
  38. return isLeapYear(year) ? 29 : 28;
  39. default:
  40. return -1;
  41. }
  42. }
  43. //是否是闰年
  44. private boolean isLeapYear(int year) {
  45. return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  46. }
  47. //将from的参数传进来
  48. public MyDate(MyDate from) {
  49. this.year = from.year;
  50. this.month = from.month;
  51. this.day = from.day;
  52. }
  53. @Override
  54. public String toString() {
  55. return "MyDate{" + "year=" + year + ", month=" + month + ", day=" + day + '}';
  56. }
  57. //计算相差天数:
  58. public int method(MyDate from) {
  59. if (this.compareTo(from) <= 0) {
  60. throw new RuntimeException("from 的日期必须当前日期之前");
  61. }
  62. MyDate fromCopy = new MyDate(from);
  63. int count = 0;
  64. while (fromCopy.compareTo(this) < 0) {
  65. System.out.println(fromCopy);
  66. fromCopy.increment();
  67. count++;
  68. }
  69. return count;
  70. //return count = between(this) - between(fromCopy);
  71. }
  72. //考虑进位
  73. private void increment() {
  74. day++;
  75. if (day <= getMonthDay(year, month)) {
  76. // day 不需要考虑进位
  77. return;
  78. }
  79. // day 需要考虑日期进位
  80. month++;
  81. day = 1;
  82. if (month <= 12) {
  83. // month 不需要考虑进位
  84. return;
  85. }
  86. year++;
  87. month = 1;
  88. }
  89. private int compareTo(MyDate from) {
  90. if (year != from.year) {
  91. return year - from.year;
  92. }
  93. if (month != from.month) {
  94. return month - from.month;
  95. }
  96. return day - from.day;
  97. }
  98. }

具体调用:

  1. public class MyDateTest {
  2. public static void main(String[] args) {
  3. MyDate from = new MyDate(2020, 1, 1);
  4. MyDate to = new MyDate(2020, 1, 3);
  5. System.out.println(to.method(from));
  6. System.out.printf("从 %s 到 %s 经过了 %d 天\n",from, to, to.method(from));
  7. }
  8. }

具体结果:

在这里插入图片描述
在这里插入图片描述

两个时间之间间隔秒数

这个题目和上面的题目思想以及用法都十分的类似,都是这样考虑的问题,
在这里我就只放源代码和结果了:

类的定义:

  1. /** *计算两个时间差 */
  2. public class Mytime {
  3. public int hour;
  4. public int min;
  5. public int sec;
  6. public Mytime(int hour, int min, int sec) {
  7. if (hour<0||hour>=24){
  8. throw new RuntimeException("hour参数不合法");
  9. }
  10. if (min<0||min>=60){
  11. throw new RuntimeException("min参数不合法");
  12. }
  13. if (sec<0||sec>=60){
  14. throw new RuntimeException("sec参数不合法");
  15. }
  16. this.hour = hour;
  17. this.min = min;
  18. this.sec = sec;
  19. }
  20. @Override
  21. public String toString() {
  22. return "Mytime{" + "hour=" + hour + ", min=" + min + ", sec=" + sec + '}';
  23. }
  24. public Mytime(Mytime fromTime){
  25. this.hour = fromTime.hour;
  26. this.min = fromTime.min;
  27. this.sec = fromTime.sec;
  28. }
  29. public int methodTime(Mytime fromTime){
  30. if (this.compareToTime(fromTime) <= 0) {
  31. throw new RuntimeException("from 的时间必须当前时间之前");
  32. }
  33. Mytime fromCopy = new Mytime(fromTime);
  34. int count = 0;
  35. while (fromCopy.compareToTime(this) < 0) {
  36. System.out.println(fromCopy);
  37. fromCopy.increment();
  38. count++;
  39. }
  40. return count;
  41. }
  42. //考虑进位
  43. private void increment() {
  44. sec++;
  45. if (sec < 60) {
  46. return;
  47. }
  48. min++;
  49. sec = 0;
  50. if (min < 60) {
  51. return;
  52. }
  53. hour++;
  54. min =0 ;
  55. }
  56. private int compareToTime(Mytime fromTime) {
  57. if (hour != fromTime.hour) {
  58. return hour - fromTime.hour;
  59. }
  60. if (min != fromTime.min) {
  61. return min - fromTime.min;
  62. }
  63. return sec - fromTime.sec;
  64. }
  65. }

类的调用

  1. public class MytimeTest {
  2. public static void main(String[] args) {
  3. Mytime fromTime=new Mytime(9,10,23);
  4. Mytime toTime=new Mytime(10,10,38);
  5. System.out.printf("从 %s 到 %s 经过了 %d 秒\n",fromTime, toTime, toTime.methodTime(fromTime));
  6. }
  7. }

具体结果

在这里插入图片描述

两个日期和时间之间间隔时间

类的定义:

  1. package DateTime;
  2. /** *计算日期 */
  3. public class MyDate {
  4. private int year;
  5. private int mon;
  6. private int day;
  7. public MyDate(MyDate Date) {
  8. this.year = Date.year;
  9. this.mon = Date.mon;
  10. this.day = Date.day;
  11. }
  12. public MyDate(int year, int mon, int day) {
  13. //判断参数的合法性
  14. cheak(year, mon, day);
  15. this.year = year;
  16. this.mon = mon;
  17. this.day = day;
  18. }
  19. @Override
  20. public String toString() {
  21. return String.format("%d-%02d-%02d", year, mon, day);
  22. }
  23. private void cheak(int year, int mon, int day) {
  24. //校验参数的合法性
  25. if (mon < 1 || mon > 12) {
  26. throw new RuntimeException("mon的范围是1-12");
  27. }
  28. int days = getMonYear(year, mon);
  29. if (day < 1 || day > days) {
  30. throw new RuntimeException("day的范围是1-" + days);
  31. }
  32. }
  33. private static int getMonYear(int year, int mon) {
  34. if (mon == 2 && IsleapYear(year)) {
  35. return 29;
  36. }
  37. return DAY[mon - 1];
  38. }
  39. private static boolean IsleapYear(int year) {
  40. return (year % 4 == 0 && year % 400 != 0) || (year % 400 == 0);
  41. }
  42. private static final int[] DAY = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  43. //往后走一天
  44. public void next() {
  45. day++;
  46. if (day <= getMonYear(year, mon)) {
  47. return;
  48. }
  49. mon++;
  50. day = 1;
  51. if (mon <= 12) {
  52. return;
  53. }
  54. year++;
  55. mon = 1;
  56. }
  57. //往前走一天
  58. public void previous() {
  59. day--;
  60. if (day > 0) {
  61. return;
  62. }
  63. mon--;
  64. if (mon > 0) {
  65. day = getMonYear(year, mon);
  66. return;
  67. }
  68. year--;
  69. mon = 12;
  70. day = getMonYear(year, mon);
  71. }
  72. }
  73. package DateTime;
  74. /** * 计算时间 */
  75. public class MyTime {
  76. private int hour;
  77. private int min;
  78. private int sec;
  79. public MyTime(MyTime time) {
  80. this.hour = time.hour;
  81. this.min = time.min;
  82. this.sec = time.sec;
  83. }
  84. public MyTime(int hour, int min, int sec) {
  85. cheak(hour, min, sec);
  86. this.hour = hour;
  87. this.min = min;
  88. this.sec = sec;
  89. }
  90. //往后走1秒
  91. public boolean next() {
  92. sec++;
  93. if (sec < 60) {
  94. return false;
  95. }
  96. min++;
  97. sec = 0;
  98. if (min < 60) {
  99. return false;
  100. }
  101. hour++;
  102. min = 0;
  103. if (hour < 24) {
  104. return false;
  105. }
  106. hour = 0;
  107. return true;
  108. }
  109. //往前走1秒
  110. public boolean previous() {
  111. sec--;
  112. if (sec >= 0) {
  113. return false;
  114. }
  115. min--;
  116. sec = 59;
  117. if (min >= 0) {
  118. return false;
  119. }
  120. hour--;
  121. min = 59;
  122. if (hour >= 0) {
  123. return false;
  124. }
  125. hour = 23;
  126. return true;
  127. }
  128. @Override
  129. public String toString() {
  130. return String.format("%02d:%02d:%02d", hour, min, sec);
  131. }
  132. //合法性校验
  133. private static void cheak(int hour, int min, int sec) {
  134. if (hour < 0 || hour >= 24) {
  135. throw new RuntimeException("hour参数有问题");
  136. }
  137. if (min < 0 || min >= 60) {
  138. throw new RuntimeException("min参数有问题");
  139. }
  140. if (sec < 0 || sec >= 60) {
  141. throw new RuntimeException("sec参数有问题");
  142. }
  143. }
  144. }
  145. package DateTime;
  146. /** * 计算时间日期 */
  147. public class DateTime {
  148. private MyDate date;
  149. private MyTime time;
  150. public DateTime(DateTime datetime) {
  151. this.date = new MyDate(datetime.date);
  152. this.time = new MyTime(0, 0, 0);
  153. }
  154. public DateTime(int year, int month, int day, int hour, int minute, int second) {
  155. this.date = new MyDate(year, month, day);
  156. this.time = new MyTime(hour, minute, second);
  157. }
  158. public DateTime(int year, int month, int day) {
  159. this(year, month, day, 0, 0, 0);
  160. }
  161. public DateTime(MyDate date) {
  162. this.date = new MyDate(date);
  163. }
  164. // 向后走一秒
  165. public void next() {
  166. if (time.next()) {
  167. date.next();
  168. }
  169. }
  170. // 向前走一秒
  171. public void previous() {
  172. if (time.previous()) {
  173. date.previous();
  174. }
  175. }
  176. @Override
  177. public String toString() {
  178. return String.format("%s %s", date, time);
  179. }
  180. }
  181. package DateTime;
  182. import java.util.concurrent.TimeUnit;
  183. public class Teat {
  184. public static void main(String[] args) throws InterruptedException {
  185. DateTime datetime = new DateTime(2021, 12, 31, 23, 59, 53);
  186. while (true) {
  187. System.out.println(datetime);
  188. datetime.next();
  189. TimeUnit.SECONDS.sleep(1); // 让代码停顿 1 秒
  190. }
  191. }
  192. }

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读