Skip to content

Instantly share code, notes, and snippets.

@villiger
Created December 15, 2012 10:14
Show Gist options
  • Save villiger/4292490 to your computer and use it in GitHub Desktop.
Save villiger/4292490 to your computer and use it in GitHub Desktop.
D&A Aufgabensatz 4 - Date
/**
* Primitive Datumsklasse in der Monate immer 30 Tage lang sind,
* es keine Schaltjahre gibt und die Zeit aussen vor gelassen
* wird. Diese Datumsklasse funktioniert nur mit Daten die jünger
* als der 1.1.2000 sind.
*
* @author Andy Villiger
*/
public class Date {
private int timestamp;
public Date(int day, int month, int year) {
if (year < 2000) { year += 2000; }
if (month < 1) { month = 1; }
if (month > 12) { month = 12; }
if (day < 1) { day = 1; }
if (day > 30) { day = 30; }
timestamp = (day - 1) + ((month - 1) * 30) + (year * 360);
}
public Date(int timestamp) {
this.timestamp = timestamp;
}
public int getTimestamp() {
return timestamp;
}
public int getDay() {
return (timestamp % 30) + 1;
}
public int getMonth() {
return (timestamp / 30 % 12) + 1;
}
public int getYear() {
return (timestamp / 360);
}
public Date next() {
return plus(1);
}
public Date previous() {
return plus(-1);
}
public Date plus(int days) {
return new Date(timestamp + days);
}
public Date between(Date other) {
return new Date((timestamp + other.getTimestamp()) / 2);
}
public boolean isBefore(Date other) {
return (timestamp < other.getTimestamp());
}
public boolean isAfter(Date other) {
return (timestamp > other.getTimestamp());
}
public boolean equal(Date other) {
return (timestamp == other.getTimestamp());
}
@Override
public String toString() {
return getDay() + "." + getMonth() + "." + getYear();
}
public static void main(String[] args) {
Date date = new Date(9, 11, 12);
System.out.println(date);
System.out.println(date.plus(60));
System.out.println(date.plus(-35));
System.out.println(date.isBefore(date.plus(1)));
System.out.println(date.isAfter(date.plus(1)));
System.out.println(date.equal(date.plus(1).plus(-1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment