package com.example.nyuscps.junk; import android.util.Log; /** * Created by nyuscps on 2/21/15. */ public class Date { /** Because of the keyword "static", one copy of this array * is shared by all the Date objects. * No matter how many Date objects are created, * there will always be exactly one copy of this array. * In fact, there will always be exactly one copy of this array * even if no Date objects are ever created. */ private static int[] a = { 0, //dummy 31, //January 29, //February: can't distinguish between leap and non-leap 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; //The three fields (internal organs) of a Date object. private int year; /** in the range 1 to 12 inclusive */ private int month; /** in the range 1 to number of days in month inclusive */ private int day; /** * The length of the current month. * This method will eventually be overridden by a smarter method * in a derived class. * * @return number of days in month to which this Date object belongs */ private int length() { return a[month]; } /** * Create a new Date object. * This method is a constructor for class Date. * * @param month in the range 1 to 12 inclusive * @param day in the range 1 to number of days in month inclusive * @param year can't be zero */ public Date(int month, int day, int year) { this.year = year; //two variables with the same name this.month = month; this.day = day; } public Date() { year = 1; month = 1; day = 1; } /** * Move this Date object one day into the future. * If already at the last possible date, do nothing. */ public void next() { if (day < length()) { ++day; return; } day = 1; if (month < 12) { ++month; return; } month = 1; if (year < Integer.MAX_VALUE) { ++year; return; } //Roll back an unsuccessful change. //Don't leave this Date object in a half-changed state. day = 31; month = 12; Log.d("mytag", "Can't go beyond " + this + "."); } public void prev() { if (day > 1) { --day; //means day = day - 1; return; } //Arrive here when day == 1. if (month > 1) { --month; } else { //Arrive here if month == 1 month = 12; --year; } day = length(); } /** * Move this Date object count days into the future. * * @param count the number of days */ public void next(int count) { if (count >= 0) { for (int i = 0; i < count; ++i) { next(); } } else { for (int i = 0; i > count; --i) { prev(); } } } /* public void prev(int count) { for (int i = 0; i < count; ++i) { prev(); } } */ /** * Return a String showing the contents of this Date object. * @return a String in the format 12/31/2011 */ public String toString() { return month + "/" + day + "/" + year; //The above statement could have been written //return this.month + "/" + this.day + "/" + this.year; //but it doesn't have to be. } }