Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.ostfalia.gdp.ss18;
- import java.util.*;
- /**
- *
- * @author Raffael, Moritz
- *
- */
- public class DateTimeBi {
- public static void main (String[]args) {
- }
- /**
- * umrechnung in millisekunden + deklaration von statischen variablen
- */
- private static final String[] DAYNAME = { "Merkur", "Venus", "Erde", "Mars", "Jupiter", "Saturn", "Uranus", "Neptunpisser" };
- private static final long MILLISEC = 1000;
- private static final long SECBI = 64; // sekunden in bi
- private static final long MIN_SEC = 64; // minuten in bi
- private static final long DAYBI = 16; // in bi
- private static final long DAYS_IN_MONTHBI = 32; // tage eines monats in bi
- private static final long HOUR_SEC = SECBI * MIN_SEC;
- private static final long DAY_SEC = DAYBI * HOUR_SEC;
- private static final long MONTH_SEC = DAYS_IN_MONTHBI * DAY_SEC;
- private static final long YEAR_IN_SEC = DAYBI * MONTH_SEC;
- private static final long YEARMILLI = MILLISEC * YEAR_IN_SEC; // für DATE
- private static final long MONTHMILLI = MILLISEC * MONTH_SEC;
- private static final long DAYMILLI = MILLISEC * DAY_SEC;
- private static final long HOURMILLI = MILLISEC * SECBI * MIN_SEC;
- private static final long MINMILLI = MILLISEC * SECBI;
- private static final long WEEKMILLI = DAYMILLI * 8;
- private long milliseconds;
- private static final long UNIX_TIME_TO_NOW = 275248380000L;
- /**
- * berechnung der Differenz in Zeit zwischen 1960/1970
- */
- public DateTimeBi() {
- this.milliseconds = System.currentTimeMillis() + UNIX_TIME_TO_NOW;
- }
- /**
- * hier wird die Gesamtzeit der eingegebenen Werte in Millis wird berechnet
- *
- * @param dayBi
- * tag
- * @param monthBi
- * monat
- * @param yearBi
- * jahr
- * @param hourBi
- * stunde
- * @param minuteBi
- * minute
- */
- public DateTimeBi(int dayBi, int monthBi, int yearBi, int hourBi, int minuteBi) {
- long allTime = yearBi * YEARMILLI + monthBi * MONTHMILLI + dayBi * DAYMILLI + hourBi * HOURMILLI
- + minuteBi * MINMILLI;
- this.milliseconds = allTime;
- }
- /**
- * Hilfsmethode
- *
- * @return exakter monat wird berechnet, nachkommastellen werden abgeschnitten.
- * Jahre werden abgezogen
- */
- public int getMonth() {
- long withoutYear = (this.milliseconds - getYear() * YEARMILLI);
- return ((int) (withoutYear / MONTHMILLI));
- }
- /**
- * Hilfsmethode
- *
- * @return exakte woche wird berechnet, nachkommastellen werden abgeschnitten.
- * Jahre und Monate werden abgezogen
- */
- public int getWeeks() {
- long withoutMonthandYEAR = (this.milliseconds - getMonth() * MONTHMILLI - getYear() * YEARMILLI);
- return ((int) (withoutMonthandYEAR / WEEKMILLI));
- }
- /**
- * Hilfsmethode
- *
- * @return exaktes Jahr wird berechnet
- */
- public int getYear() {
- return ((int) (this.milliseconds / YEARMILLI));
- }
- /**
- * Hilfsmethode
- *
- * @return exakter Tag wird berechnet, monate und jahre werden abgezogen
- */
- public int getDay() { // tage eines Monats im Prinzip modulo
- long withoutMonthandYEAR = (this.milliseconds - getMonth() * MONTHMILLI - getYear() * YEARMILLI);
- return ((int) (withoutMonthandYEAR / DAYMILLI));
- }
- // erzeugt ein neues Object mit dem selben Tag wie day, aber HourBi und MinBi
- // neu gesetzt
- /**
- * "roher" Tag wird berechnet und hourBi und minBi werden hinzugefügt
- *
- * @param day
- * aus vorherigen Methoden übernommen
- * @param hourBi
- * eingegebene Stunden
- * @param minBi
- * eingegebene Minuten
- */
- public DateTimeBi(DateTimeBi day, int hourBi, int minBi) {
- long newRawDay = day.milliseconds;
- newRawDay = day.getYear() * YEARMILLI + day.getMonth() * MONTHMILLI + day.getDay() * DAYMILLI; // neuer Tag um
- // 00:00
- // this.milliseconds = (
- // MILLISEC * SEK_PER_MINBI * MIN_PER_HOURBI * HOURS_PER_DAYBI);
- //
- // long milliseconds = (hourBi * MIN_PER_HOURBI) + minBi; // stunden neu
- // berechnet
- // milliseconds *= SEK_PER_MINBI * MILLISEC;
- this.milliseconds = newRawDay + hourBi * HOURMILLI + minBi * MINMILLI;
- }
- // public static DateTimeBi today(int hourBi, int minBi) {
- // DateTimeBi a = new DateTimeBi();
- //
- // return new DateTimeBi(a, hourBi, minBi);
- // }
- /**
- *
- * @param hourBi
- * wert
- * @param minBi
- * wert
- * @return heutiger Tag mit neuer Stunde und neuen Minuten
- */
- public static DateTimeBi today(int hourBi, int minBi) {
- DateTimeBi toDaynow = new DateTimeBi();
- return new DateTimeBi(toDaynow, hourBi, minBi);
- }
- /**
- * Hilfsmethode
- *
- * @param milliseconds
- * werden zurückgesetzt
- */
- private DateTimeBi(long milliseconds) {
- this.milliseconds = milliseconds;
- }
- /**
- *
- * @param number
- * wert
- * @return Datum + anzahl der Tage (number)
- */
- public DateTimeBi daysBiLater(int number) {
- long newMILLISEC = this.milliseconds;
- newMILLISEC += (number * DAYMILLI);
- return new DateTimeBi(newMILLISEC);
- }
- /**
- * Datum als String liefern (hexadezimal DayBi.MonthBi.YearBi),
- *
- * @return datum als string
- */
- public String getDate() {
- return Integer.toHexString(getDay()) + "." + Integer.toHexString(getMonth()) + "."
- + Integer.toHexString(getYear());
- }
- /**
- * zeit wird als hexa zurück gegeben
- *
- * @return zeit
- */
- public String getTime() {
- long rawTimeMilli = (this.milliseconds
- - (getYear() * YEARMILLI + getMonth() * MONTHMILLI + getDay() * DAYMILLI));
- int hours = (int) (rawTimeMilli / HOURMILLI);
- int mins = (int) ((rawTimeMilli - hours * HOURMILLI) / MINMILLI);
- // return Integer.toHexString(hours) + ":" + Integer.toHexString(mins);
- return String.format("%02x:%02x", hours, mins);
- }
- /**
- *
- * @return wochentag als Zahl
- */
- public int dayOfWeekInt() {
- int weekday = (getDay()) % 8;
- return weekday;
- }
- /**
- *
- * @return wochentag als string + "Tag"
- */
- public String dayOfWeek() {
- return DAYNAME[dayOfWeekInt()] + "Tag";
- }
- /**
- * differenz berechnen
- *
- * @param start
- * bestimmtes Datum
- * @return differenz zwischen jetzt und Datum (start)
- */
- public long getDuration(DateTimeBi start) {
- long diff = Math.abs(this.milliseconds - start.milliseconds);
- return diff;
- }
- /**
- * Datum, Uhrzeit und Tag sollen ausgegeben werden
- *
- * @param start
- * bestimmtes Datum
- * @return alles wird als String ausgegeben
- */
- public String getDurationString(DateTimeBi start) {
- DateTimeBi diffy = new DateTimeBi(getDuration(start));
- int years = diffy.getYear();
- int months = diffy.getMonth();
- int weeks = diffy.getWeeks();
- int days = diffy.dayOfWeekInt();// + 1
- String time = diffy.getTime();
- StringBuffer duration = new StringBuffer();
- if (years > 0) {
- duration.append(years + " YearsBi ");// append zusammenfügen
- }
- if (months > 0) {
- duration.append(months + " MonthsBi ");
- }
- if (weeks > 0) {
- duration.append(weeks + " WeekBi ");
- }
- if (days > 0) {
- duration.append(days + " DayBi ");
- }
- // if (hours > 0)
- // duration.append(hours + ":");
- duration.append(time);
- return duration.toString();
- }
- /**
- * String wird überschrieben
- *
- * @return zeug
- */
- public String toString() {
- return dayOfWeek() + ", " + getDate() + " " + getTime();
- }
- /**
- *
- * @param other
- * zweite Zeit
- * @return true/ false, ob danach
- */
- public boolean isAfter(DateTimeBi other) {
- return (milliseconds >= other.milliseconds);
- }
- /**
- *
- * @param other
- * zweite Zeit
- * @return true/ false, ob davor
- */
- public boolean isBefore(DateTimeBi other) {
- return (milliseconds <= other.milliseconds);
- }
- /**
- * wie oben, nur wird jetzt lediglich noch auf den Tag/ Stunden/ etc. geschaut
- *
- * @param other
- * anderes Datum
- * @param ignoreWeek
- * woche soll außen vor gelassen werden
- * @return true/ false
- */
- public boolean isAfter(DateTimeBi other, boolean ignoreWeek) {
- long millinew = milliseconds;
- long othermilli = other.milliseconds;
- if (ignoreWeek) {
- millinew -= getYear() * YEARMILLI + getMonth() * MONTHMILLI + getWeeks() * WEEKMILLI;
- othermilli -= other.getYear() * YEARMILLI + other.getMonth() * MONTHMILLI + other.getWeeks() * WEEKMILLI;
- }
- return (millinew >= othermilli);
- }
- /**
- * wie oben, nur wird jetzt lediglich noch auf den Tag/ Stunden/ etc. geschaut
- *
- * @param other
- * anderes Datum
- * @param ignoreWeek
- * woche soll außen vor gelassen werden
- * @return true/ false
- */
- public boolean isBefore(DateTimeBi other, boolean ignoreWeek) {
- long millinew = milliseconds;
- long othermilli = other.milliseconds;
- if (ignoreWeek) {
- millinew -= getYear() * YEARMILLI + getMonth() * MONTHMILLI + getWeeks() * WEEKMILLI;
- othermilli -= other.getYear() * YEARMILLI + other.getMonth() * MONTHMILLI + other.getWeeks() * WEEKMILLI;
- }
- // if (milliseconds < other.milliseconds) {
- // return true;
- // }else if(milliseconds == other.milliseconds){
- // return true;
- // }else {
- // return false;
- // }
- return (millinew <= othermilli);
- }
- /**
- * kalender in unserer zeit
- *
- * @param cal
- * kalender
- */
- public DateTimeBi(Calendar cal) {
- this.milliseconds = cal.getTimeInMillis() + UNIX_TIME_TO_NOW;
- }
- /**
- *
- * @return erdzeit im kalender im vergleich zu weltraumzeit
- */
- public Calendar earthTime() {
- Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- cal.setTimeInMillis(milliseconds - UNIX_TIME_TO_NOW);
- return cal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement