Java implementation of a Gregorian Date: validation (month lengths, leap years), comparison (before/after/equal), day difference, and next-day calculation; clean OOP API.
- Name: Shimon Esterkin (SemionVlad)
- ID: *****2258
- Semester: 2023B
The Date class provides a robust implementation of date handling based on the Gregorian calendar system. It supports date validation, comparison, and calculations while handling special cases like leap years and month-specific day limits.
Date.java
├── Constants
│ └── Month Constants (JANUARY through DECEMBER)
├── Instance Variables
│ ├── day (private int)
│ ├── month (private int)
│ └── year (private int)
├── Helper Methods
│ ├── ifThirtyDays()
│ ├── ifThirtyOneDays()
│ ├── isLeapYear()
│ ├── verifyDate()
│ └── calculateDate()
└── Public Interface
├── Constructors
├── Getters/Setters
├── Comparison Methods
└── Utility Methods
-
Date Validation
- Comprehensive date verification
- Leap year handling
- Month-specific day limits
-
Date Comparison
- Equality checking
- Before/after comparisons
- Day difference calculation
-
Date Manipulation
- Next day computation
- Date copying
- Individual component updates
// Create a new date
Date date1 = new Date(25, 12, 2023);
// Copy an existing date
Date date2 = new Date(date1);
// Invalid date defaults to 01/01/2000
Date invalidDate = new Date(31, 2, 2023);
// Check if one date is before another
boolean isBefore = date1.before(date2);
// Check if dates are equal
boolean areEqual = date1.equals(date2);
// Calculate days between dates
int daysDiff = date1.difference(date2);
// Get tomorrow's date
Date tomorrow = date1.tomorrow();
// Modify date components
date1.setDay(15);
date1.setMonth(6);
date1.setYear(2024);
- Year limits: 0-9999
- Month limits: 1-12
- Day limits:
- 31 days: Jan, Mar, May, Jul, Aug, Oct, Dec
- 30 days: Apr, Jun, Sep, Nov
- 28/29 days: Feb (leap year dependent)
- Year divisible by 4 is a leap year
- Century years must be divisible by 400
- Examples:
- 2000: Leap year (divisible by 400)
- 2100: Not a leap year (divisible by 100 but not 400)
- 2024: Leap year (divisible by 4)
Date(int day, int month, int year)
- Creates new dateDate(Date other)
- Creates copy of existing date
getDay()
,getMonth()
,getYear()
- Return componentssetDay(int)
,setMonth(int)
,setYear(int)
- Set components
equals(Date)
- Checks date equalitybefore(Date)
- Checks if date is earlierafter(Date)
- Checks if date is laterdifference(Date)
- Calculates days between dates
tomorrow()
- Returns next day's datetoString()
- Returns "DD/MM/YYYY" format
- Invalid dates in constructor default to 01/01/2000
- Invalid component updates are ignored
- All operations maintain date validity
- Basic operations are O(1)
- Date calculations optimize for common cases
- Memory efficient implementation
- Java Development Kit (JDK)
- No external dependencies
-
Test date validation
- Valid dates in different months
- Leap year dates
- Invalid dates
- Edge cases (year boundaries)
-
Test comparisons
- Equal dates
- Sequential dates
- Dates across year boundaries
-
Test calculations
- Day differences
- Tomorrow calculations
- Leap year transitions
- No time zone support
- Limited to years 0-9999
- No BC dates support
- No partial date support
- Support for different calendar systems
- Extended year range
- Time zone support
- Date parsing from strings
- Date formatting options
Created by SemionVlad for academic and portfolio purposes. This repository is intended for non-commercial, educational sharing only.