Nov 19

## Zeller’s Congruence in Java

You can use the Calendar class to get the day of the week for a particular date. But if you’re after an algorithm to do the same then Zeller’s congruence may be of interest to you.

Zeller’s congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any Julian or Gregorian calendar date.

Here’s an implementation of the Zeller algorithm in Java.

```
Calendar today = Calendar.getInstance();
int day = today.get(Calendar.DATE);
int month = today.get(Calendar.MONTH) + 1;
int year = today.get(Calendar.YEAR);

if (month < 3) {
month += 12;
year -= 1;
}

int k = year % 100;
int j = year / 100;

// 0 = Saturday, 1 = Sunday, ...
int dayOfWeek = ((day + (((month + 1) * 26) / 10) +
k + (k / 4) + (j / 4)) + (5 * j)) % 7;
```

Hope this helps you implement Zeller in Java.

written by objects \\ tags: , , , , ,

Sep 13

## How do I add a specified number of minutes to a Java Date?

The Calendar class can be used to do a variety of date arithmetic

```
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, numberOfMinutes);
date = cal.getTime();

```

written by objects \\ tags: , ,

Sep 13

## How do I add a specified number of months to a Java Date?

The Calendar class can be used to do a variety of date arithmetic

```
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, numberOfMonths);
date = cal.getTime();

```

written by objects \\ tags: ,