I need to implement gregorian calendar in java
Please give me ideas how to start and what to use?

Dani AI

Generated

and correctly pointed to the built-in java.util.GregorianCalendar, but since requested a manual implementation, a compact, reliable approach is to implement three primitives: a correct leap-year test, conversion between (year, month, day) and an absolute day count (Julian Day Number), and normalization routines for arithmetic (add/subtract days, months, years). Using an absolute-day representation makes comparisons, addition and day-of-week trivial and less error-prone than juggling month lengths directly.

The following Java helpers implement the core conversions (Fliegel–Van Flandern style) and a day-of-week routine. Month is 1..12, day is 1..31; all arithmetic uses long to avoid overflow for large year ranges.

public static boolean isLeapGregorian(int y) {
    return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0);
}

public static long toJulianDayNumber(int year, int month, int day) {
    int a = (14 - month) / 12;
    int y = year + 4800 - a;
    int m = month + 12 * a - 3;
    return day + (153L * m + 2) / 5 + 365L * y + y / 4 - y / 100 + y / 400 - 32045L;
}

public static int[] fromJulianDayNumber(long jdn) {
    long l = jdn + 68569;
    long n = 4 * l / 146097;
    l = l - (146097 * n + 3) / 4;
    long i = 4000 * (l + 1) / 1461001;
    l = l - 1461 * i / 4 + 31;
    long j = 80 * l / 2447;
    int day = (int)(l - 2447 * j / 80);
    l = j / 11;
    int month = (int)(j + 2 - 12 * l);
    int year = (int)(100 * (n - 49) + i + l);
    return new int[] { year, month, day };
}

public static int dayOfWeekFromJdn(long jdn) {
    int d = (int)((jdn + 1) % 7); // 0=Sunday .. 6=Saturday
    return d < 0 ? d + 7 : d;
}

Notes and pitfalls: decide whether the implementation should be proleptic Gregorian (apply Gregorian rules to all years) or model the historical Julian->Gregorian cutover (Java’s default change date is 1582-10-15). Historical counting has no year zero; many algorithms use astronomical year numbering (year 0 = 1 BC) to simplify math. Month-addition semantics must clamp the day-of-month (e.g., Jan 31 -> Feb 28/29). Useful test cases: 1582-10-04 / 1582-10-15 (cutover), 1900 (not leap), 2000 (leap), 1970-01-01 (Unix epoch).

References: Java’s GregorianCalendar Javadoc (Java SE 8 API) and the Julian Day conversion notes (Julian day — calculation). For production code on modern Java, prefer java.time.LocalDate (ISO/proleptic-Gregorian) unless a custom behavior is required.

Recommended Answers

All 5 Replies

Sun have saved you the bother - see java.util.GregorianCalendar in the Java API.
If you have to do this yourself then a read through the preamble to the doc for java.util.GregorianCalendar will give you a good intro into what you need to achieve.

Uhm, the GregorianCalendar class?

Edit: Damn! Too slow!

yes i dont want to use GregrianCalendar class. Need implement without that..

Okay? And have you read the intro to that class in the API docs which detail what's entailed in that? As was already suggested? And where is your attempt at all this?

I need to implement gregorian calendar in java
yes i dont want to use GregrianCalendar class. Need implement without that

please don't forget to tell us who finally won

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.