1.

 package ADT;

 import algorithms.util.StdOut;

 /******************************************************************************
* Compilation: javac Date.java
* Execution: java Date
* Dependencies: StdOut.java
*
* An immutable data type for dates.
*
******************************************************************************/ /**
* The <tt>Date</tt> class is an immutable data type to encapsulate a
* date (day, month, and year).
* <p>
* For additional documentation,
* see <a href="http://algs4.cs.princeton.edu/12oop">Section 1.2</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class Date implements Comparable<Date> {
private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private final int month; // month (between 1 and 12)
private final int day; // day (between 1 and DAYS[month]
private final int year; // year /**
* Initializes a new date from the month, day, and year.
* @param month the month (between 1 and 12)
* @param day the day (between 1 and 28-31, depending on the month)
* @param year the year
* @throws IllegalArgumentException if this date is invalid
*/
public Date(int month, int day, int year) {
if (!isValid(month, day, year)) throw new IllegalArgumentException("Invalid date");
this.month = month;
this.day = day;
this.year = year;
} /**
* Initializes new date specified as a string in form MM/DD/YYYY.
* @param date the string representation of this date
* @throws IllegalArgumentException if this date is invalid
*/
public Date(String date) {
String[] fields = date.split("/");
if (fields.length != 3) {
throw new IllegalArgumentException("Invalid date");
}
month = Integer.parseInt(fields[0]);
day = Integer.parseInt(fields[1]);
year = Integer.parseInt(fields[2]);
if (!isValid(month, day, year)) throw new IllegalArgumentException("Invalid date");
} /**
* Return the month.
* @return the month (an integer between 1 and 12)
*/
public int month() {
return month;
} /**
* Returns the day.
* @return the day (an integer between 1 and 31)
*/
public int day() {
return day;
} /**
* Returns the year.
* @return the year
*/
public int year() {
return year;
} // is the given date valid?
private static boolean isValid(int m, int d, int y) {
if (m < 1 || m > 12) return false;
if (d < 1 || d > DAYS[m]) return false;
if (m == 2 && d == 29 && !isLeapYear(y)) return false;
return true;
} // is y a leap year?
private static boolean isLeapYear(int y) {
if (y % 400 == 0) return true;
if (y % 100 == 0) return false;
return y % 4 == 0;
} /**
* Returns the next date in the calendar.
*
* @return a date that represents the next day after this day
*/
public Date next() {
if (isValid(month, day + 1, year)) return new Date(month, day + 1, year);
else if (isValid(month + 1, 1, year)) return new Date(month + 1, 1, year);
else return new Date(1, 1, year + 1);
} /**
* Compares two dates chronologically.
*
* @param that the other date
* @return <tt>true</tt> if this date is after that date; <tt>false</tt> otherwise
*/
public boolean isAfter(Date that) {
return compareTo(that) > 0;
} /**
* Compares two dates chronologically.
*
* @param that the other date
* @return <tt>true</tt> if this date is before that date; <tt>false</tt> otherwise
*/
public boolean isBefore(Date that) {
return compareTo(that) < 0;
} /**
* Compares two dates chronologically.
*
* @return the value <tt>0</tt> if the argument date is equal to this date;
* a negative integer if this date is chronologically less than
* the argument date; and a positive ineger if this date is chronologically
* after the argument date
*/
@Override
public int compareTo(Date that) {
if (this.year < that.year) return -1;
if (this.year > that.year) return +1;
if (this.month < that.month) return -1;
if (this.month > that.month) return +1;
if (this.day < that.day) return -1;
if (this.day > that.day) return +1;
return 0;
} /**
* Returns a string representation of this date.
*
* @return the string representation in the format MM/DD/YYYY
*/
@Override
public String toString() {
return month + "/" + day + "/" + year;
} /**
* Compares this date to the specified date.
*
* @param other the other date
* @return <tt>true</tt> if this date equals <tt>other</tt>; <tt>false</tt> otherwise
*/
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null) return false;
if (other.getClass() != this.getClass()) return false;
Date that = (Date) other;
return (this.month == that.month) && (this.day == that.day) && (this.year == that.year);
} /**
* Returns an integer hash code for this date.
*
* @return a hash code for this date
*/
@Override
public int hashCode() {
int hash = 17;
hash = 31*hash + month;
hash = 31*hash + day;
hash = 31*hash + year;
return hash;
} /**
* Unit tests the <tt>Date</tt> data type.
*/
public static void main(String[] args) {
Date today = new Date(2, 25, 2004);
StdOut.println(today);
for (int i = 0; i < 10; i++) {
today = today.next();
StdOut.println(today);
} StdOut.println(today.isAfter(today.next()));
StdOut.println(today.isAfter(today));
StdOut.println(today.next().isAfter(today)); Date birthday = new Date(10, 16, 1971);
StdOut.println(birthday);
for (int i = 0; i < 10; i++) {
birthday = birthday.next();
StdOut.println(birthday);
}
} }

最新文章

  1. .NET缓存框架CacheManager在混合式开发框架中的应用(1)-CacheManager的介绍和使用
  2. 十五天精通WCF——第一天 三种Binding让你KO80%的业务
  3. 关于ScheduledExecutorService执行一段时间之后就不执行的问题
  4. windows系统调用 遍历进程的虚拟地址
  5. IOS 自定义按钮(代码实现)+九宫格
  6. JAVABEAN连接各数据库
  7. WINDOWS渗透与提权总结(2)
  8. 车牌识别LPR(七)-- 字符特征
  9. 【转】六年软件测试感悟-从博彦到VMware
  10. Java 判断操作系统类型(适用于各种操作系统)
  11. 数据持久化之CoreData
  12. XCode 7上传遇到ERROR ITMS-90535 Unexpected CFBundleExecutable Key. 的解决办法
  13. contos 7/redhat 7 安装mysql
  14. JavaScript树(一) 简介
  15. 【嵌入式开发】 Bootloader 详解 ( 代码环境 | ARM 启动流程 | uboot 工作流程 | 架构设计)
  16. 浅析Android Dialog中setContentView()方法
  17. python基础之Day7part1集合
  18. BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
  19. 举例说明Unicode 和UTF-8之间的转换
  20. iOS 内存管理,ARC

热门文章

  1. LeetCode OJ:Unique Paths(唯一路径)
  2. explicit修饰构造函数
  3. 打印a*a的乘法表
  4. XMLSchema验证
  5. HihoCoder1465 重复旋律8(后缀自动机)
  6. cut---Linux下文本处理五大神器之四
  7. 取余运算(mod)(分治)
  8. js相关命令
  9. Linux终端录屏与播放 script 命令
  10. JavaScript实现继承的几种重要范式