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

算法Sedgewick第四版-第1章基础-003一封装日期的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-006一封装输出(文件)

    1. package algorithms.util; /*********************************************************************** ...

  2. 算法Sedgewick第四版-第1章基础-005一封装输入(可以文件,jar包里的文件或网址)

    1. package algorithms.util; /*********************************************************************** ...

  3. 算法Sedgewick第四版-第1章基础-004一封装交易对象

    1. package ADT; /****************************************************************************** * Co ...

  4. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  8. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

随机推荐

  1. 分布式事务_02_2PC框架raincat源码解析-启动过程

    一.前言 上一节已经将raincat demo工程运行起来了,这一节来分析下raincat启动过程的源码 主要包括: 事务协调者启动过程 事务参与者启动过程 二.协调者启动过程 主要就是在启动类中通过 ...

  2. Eclipse_插件_05_自动下载jar包源码插件

    一.Java Source Attacher 1.下载 官网:http://marketplace.eclipse.org/content/java-source-attacher#.U5RmTePp ...

  3. rabbitmq_学习_00_资源帖

    一.精选资料 二.参考资料 1.RabbitMQ Simplest Queue 2.RabbitMQ系列教程 2.RabbitMQ入门教程 For Java[1] - Hello World 2.Ra ...

  4. 三目运算符与if else的运行效率

    两者的效率比较: 当比较次数较少时,效率一样: 当比较次数较多时,发现ifelse的速度更快,应该是其汇编指令更少的原因.   if else的汇编代码如下:     三目运算符代码如下:

  5. A*B problem(FFT)

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  6. Java 利用Gson将json字符串转换为List<Map<String, String>>

    json字符串类似于: [ { "userPhone": "123", "userNo": "123-2", " ...

  7. UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)

    题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...

  8. SQL Server 索引中include

    SQL Server 索引中include的魅力(具有包含性列的索引) http://www.cnblogs.com/gaizai/archive/2010/01/11/1644358.html 开文 ...

  9. shell变量扩展技巧

    SHELL中有一些变量扩展的技巧,做下归纳总结 1.取字符串slice规则一:${变量名:位置起点}含义:由指定的位置起点开始,截取子字符串到字符串结束例如: var="/etc/passw ...

  10. 服务端缓存页面及IIS缓存设置

    缓存信息基本概念 我们在看网页的header信息时,经常看到这几个参数:Expires.Cache-Control.Last-Modified.ETag,它们是RFC 2616(HTTP/1.1)协议 ...