Time Period : Time « Development Class « Java






Time Period

//package jsslib.util; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; /** * Dieses Objekt reprsentiert einen Zeitraum * * @author Robert Schuster */publicclass TimePeriod implements Serializable { privatestaticfinallong serialVersionUID = 814416355886171850L; public GregorianCalendar from; public GregorianCalendar to; // Konstanten fr der Typ des Zeitraums publicfinalstaticint TODAY = 0; publicfinalstaticint YESTERDAY = 1; publicfinalstaticint THIS_WEEK = 2; publicfinalstaticint LAST_WEEK = 3; publicfinalstaticint THIS_MONTH = 4; publicfinalstaticint LAST_MONTH = 5; publicfinalstaticint THIS_JEAR = 6; publicfinalstaticint LAST_JEAR = 7; publicfinalstaticint EVER = 8; publicint typ = -1; publicint from_typ = -1; publicint to_typ = -1; // Ein Tag in Millisekunden: publicfinalstaticlong ONE_DAY = 86400000L; // One hour in ms publicfinalstaticlong ONE_HOUR = 3600000L; // One minute in ms publicfinalstaticlong ONE_MIN = 60000L; /** * Ohne einen Parameter wird von und bis auf jetzt gesetzt */public TimePeriod() { from = new GregorianCalendar(); to = new GregorianCalendar(); } /** * Von und bis werden auf einen Zeitraum gesetzt, der durch den typ bestimmt * ist * * @param typ * siehe Konstanten! */public TimePeriod(int typ) { this.typ = typ; this.from_typ = typ; this.to_typ = typ; setFromTyp(typ); setToTyp(typ); } /** * Setzt den von-Wert auf einen der Standard-Typen * * @param neuertyp */publicvoid setFromTyp(int neuertyp) { this.from_typ = neuertyp; if (from_typ != to_typ) typ = -1; else typ = neuertyp; switch (neuertyp) { case TODAY: from = new GregorianCalendar(); setMidnight(from); break; case YESTERDAY: from = new GregorianCalendar(); setMidnight(from); from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY); break; case THIS_WEEK: from = new GregorianCalendar(); setMidnight(from); int day_of_week = from.get(Calendar.DAY_OF_WEEK); // unsere Woche beginnt am Montag. Montag hat den Wert 2 int day_offset_von; // wenn es sonntag ist, wird die zurck liegende woche betrachtet if (day_of_week == 1) { day_offset_von = -6; } else { day_offset_von = 2 - day_of_week; } // bis ist logischerweise 6-Tage nach von  from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY * day_offset_von); break; case LAST_WEEK: // wie diese woche, nur 7 tage weiter zurck  from = new GregorianCalendar(); setMidnight(from); int day_of_week2 = from.get(Calendar.DAY_OF_WEEK); // unsere Woche beginnt am Montag. Montag hat den Wert 2 int day_offset_von2; // wenn es sonntag ist, wird die zurck liegende woche betrachtet if (day_of_week2 == 1) { day_offset_von2 = -13; } else { day_offset_von2 = -5 - day_of_week2; } from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY * day_offset_von2); break; case THIS_MONTH: from = new GregorianCalendar(); setMidnight(from); from.set(Calendar.DAY_OF_MONTH, 1); break; case LAST_MONTH: from = new GregorianCalendar(); setMidnight(from); from.set(Calendar.DAY_OF_MONTH, 1); // der erste tag des letzten Monats ist vielleicht nicht mehr in // diesem Jahr, also // rckwrts laufen, bis wieder ein erster Tag gefunden wird  from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY); while (from.get(Calendar.DAY_OF_MONTH) != 1) from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY); break; case THIS_JEAR: from = new GregorianCalendar(); setMidnight(from); from.set(Calendar.DAY_OF_MONTH, 1); from.set(Calendar.MONTH, 0); break; case LAST_JEAR: from = new GregorianCalendar(); int jahr = from.get(Calendar.YEAR); jahr--; setMidnight(from); from.set(Calendar.DAY_OF_MONTH, 1); from.set(Calendar.MONTH, 0); from.set(Calendar.YEAR, jahr); break; case EVER: from = new GregorianCalendar(); int jahr2 = from.get(Calendar.YEAR); setMidnight(from); from.set(2000, 0, 1, 0, 0, 0); break; } // Von darf nicht nach bis liegen, ist bis schon initialisiert, so muss // es angepasst werden if (to != null) if (to.before(from)) { to.setTimeInMillis(from.getTimeInMillis()); set2359(to); if (from_typ == TODAY) to_typ = TODAY; } } /** * Setzt den bis-Wert auf einen der Standard-Typen * * @param neuertyp */publicvoid setToTyp(int neuertyp) { this.to_typ = neuertyp; if (from_typ != to_typ) typ = -1; else typ = neuertyp; switch (neuertyp) { case TODAY: to = new GregorianCalendar(); set2359(to); break; case YESTERDAY: to = new GregorianCalendar(); set2359(to); to.setTimeInMillis(to.getTimeInMillis() - ONE_DAY); break; case THIS_WEEK: to = new GregorianCalendar(); int day_of_week = to.get(Calendar.DAY_OF_WEEK); // unsere Woche beginnt am Montag. Montag hat den Wert 2 int day_offset_von; // wenn es sonntag ist, wird die zurck liegende woche betrachtet if (day_of_week == 1) { day_offset_von = -6; } else { day_offset_von = 2 - day_of_week; } // bis ist logischerweise 6-Tage nach von int day_offset_bis = day_offset_von + 6; set2359(to); to.setTimeInMillis(to.getTimeInMillis() + ONE_DAY * day_offset_bis); break; case LAST_WEEK: // wie diese woche, nur 7 tage weiter zurck  to = new GregorianCalendar(); int day_of_week2 = to.get(Calendar.DAY_OF_WEEK); // unsere Woche beginnt am Montag. Montag hat den Wert 2 int day_offset_von2; // wenn es sonntag ist, wird die zurck liegende woche betrachtet if (day_of_week2 == 1) { day_offset_von2 = -13; } else { day_offset_von2 = -5 - day_of_week2; } // bis ist logischerweise 6-Tage nach von int day_offset_bis2 = day_offset_von2 + 6; set2359(to); to.setTimeInMillis(to.getTimeInMillis() + ONE_DAY * day_offset_bis2); break; case THIS_MONTH: GregorianCalendar temp = new GregorianCalendar(); setMidnight(temp); temp.set(Calendar.DAY_OF_MONTH, 1); to = new GregorianCalendar(); set2359(to); // wann der letzte tag im monat ist ist unklar, also solange weiter // gehen, // bis der neue monat anfngt, dann einen tag zurck while (to.get(Calendar.MONTH) == temp.get(Calendar.MONTH)) to.setTimeInMillis(to.getTimeInMillis() + ONE_DAY); // Jetzt wieder einen tag zurck  to.setTimeInMillis(to.getTimeInMillis() - ONE_DAY); break; case LAST_MONTH: GregorianCalendar temp2 = new GregorianCalendar(); setMidnight(temp2); temp2.set(Calendar.DAY_OF_MONTH, 1); // der erste tag des letzten Monats ist vielleicht nicht mehr in // diesem Jahr, also // rckwrts laufen, bis wieder ein erster Tag gefunden wird  temp2.setTimeInMillis(temp2.getTimeInMillis() - ONE_DAY); to = new GregorianCalendar(); to.setTimeInMillis(temp2.getTimeInMillis()); set2359(to); break; case THIS_JEAR: to = new GregorianCalendar(); set2359(to); to.set(Calendar.MONTH, 11); to.set(Calendar.DAY_OF_MONTH, 31); break; case LAST_JEAR: to = new GregorianCalendar(); int jahr = to.get(Calendar.YEAR); jahr--; set2359(to); to.set(Calendar.MONTH, 11); to.set(Calendar.DAY_OF_MONTH, 31); to.set(Calendar.YEAR, jahr); break; case EVER: to = new GregorianCalendar(); int jahr2 = to.get(Calendar.YEAR); set2359(to); to.set(Calendar.MONTH, 11); to.set(Calendar.DAY_OF_MONTH, 31); to.set(Calendar.YEAR, jahr2); break; } // Von darf nicht nach bis liegen, ist bis schon initialisiert, so muss // es angepasst werden if (from != null) if (to.before(from)) { from.setTimeInMillis(to.getTimeInMillis()); setMidnight(from); from_typ = to_typ; } } /** * Erniedrigt den Von-Wert um einen Tag und prft, ob es sich dabei dann um * heute handelt. */publicvoid setFromMinusOneDay() { from.setTimeInMillis(from.getTimeInMillis() - ONE_DAY); GregorianCalendar heute = new GregorianCalendar(); setMidnight(heute); if (from.getTimeInMillis() == heute.getTimeInMillis()) from_typ = TODAY; else from_typ = -1; if (to.before(from)) { to.setTimeInMillis(from.getTimeInMillis()); set2359(to); to_typ = from_typ; } } /** * Erhht den Von-Wert um einen Tag und prft, ob es sich dabei dann um * heute handelt. */publicvoid setFromPlusOneDay() { from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY); GregorianCalendar heute = new GregorianCalendar(); setMidnight(heute); if (from.getTimeInMillis() == heute.getTimeInMillis()) from_typ = TODAY; else from_typ = -1; // Bis darf nicht vor von liegen! Falls es doch der Fall sein sollte, // wird auch bis angepasst if (to.before(from)) { to.setTimeInMillis(from.getTimeInMillis()); set2359(to); to_typ = from_typ; } } /** * Erniedrigt den Von-Wert um einen Tag und prft, ob es sich dabei dann um * heute handelt. */publicvoid setToMinusOneDay() { to.setTimeInMillis(to.getTimeInMillis() - ONE_DAY); GregorianCalendar heute = new GregorianCalendar(); set2359(heute); if (to.getTimeInMillis() == heute.getTimeInMillis()) to_typ = TODAY; else to_typ = -1; // Bis darf nicht vor von liegen! Falls es doch der Fall sein sollte, // wird auch von angepasst if (to.before(from)) { from.setTimeInMillis(to.getTimeInMillis()); setMidnight(from); from_typ = to_typ; } } /** * Erhht den Von-Wert um einen Tag und prft, ob es sich dabei dann um * heute handelt. */publicvoid setToPlusOneDay() { to.setTimeInMillis(to.getTimeInMillis() + ONE_DAY); GregorianCalendar heute = new GregorianCalendar(); set2359(heute); if (to.getTimeInMillis() == heute.getTimeInMillis()) to_typ = TODAY; else to_typ = -1; // Bis darf nicht vor von liegen! Falls es doch der Fall sein sollte, // wird auch von angepasst if (to.before(from)) { from.setTimeInMillis(to.getTimeInMillis()); setMidnight(from); } } /** * Setzt die Uhr des bergebenen Kalenders auf 00:00:00:000 * * @param cal */publicvoid setMidnight(GregorianCalendar cal) { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } /** * Setzt die Uhr des bergebenen Kalenders auf 23:59:59:999 * * @param cal */publicvoid set2359(GregorianCalendar cal) { cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); } /** * Gibt das Datum des von-Objekts als String zurck * * @return Format: */public String getFromString() { if (from == null) return"null"; SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy HH:mm"); return formater.format(from.getTime()); } /** * Gibt das Datum des von-Objekts als String zurck * * @return Format: */public String getToString() { if (to == null) return"null"; SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy HH:mm"); return formater.format(to.getTime()); } public String getFromToTimeOfDayString() { if (to == null || from == null) return"null"; SimpleDateFormat formater = new SimpleDateFormat("HH:mm"); return formater.format(from.getTime()) + " - " + formater.format(to.getTime()); } /** * Gibt das Von-Datum in der Formatierung fr die Datenbank zurck * * @return 'yyyy-MM-dd HH:mm:ss' */public String getDatabaseFromString() { if (from == null) return"0000-01-01 00:00:00"; SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return"'" + formater.format(from.getTime()) + "'"; } /** * Gibt das Bis-Datum in der Formatierung fr die Datenbank zurck * * @return 'yyyy-MM-dd HH:mm:ss' */public String getDatabaseToString() { if (to == null) return"0000-01-01 00:00:00"; SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return"'" + formater.format(to.getTime()) + "'"; } /** * Gibt die den Zeitabstand zwischen von und bis in Millisekunden zurck * * @return */publiclong getDurationInMillis() { return to.getTimeInMillis() - from.getTimeInMillis(); } /** * Gibt die den Zeitabstand zwischen von und bis in Minuten zurck * * @return */publicint getDurationInMinutes() { return (int) ((to.getTimeInMillis() - from.getTimeInMillis()) / 60000); } publicint getDurationInDays() { int ergebnis = 0; int bistag = (int) (to.getTimeInMillis() / ONE_DAY); int vontag = (int) (from.getTimeInMillis() / ONE_DAY); ergebnis = bistag - vontag + 1; return ergebnis; } /** * Krzt den zeitraum, so das nur ganze Monate brig bleiben */publicvoid setOnlyCompleteMonth() { // Auf den Beginn des nchsten monats setzen if (from.get(Calendar.DAY_OF_MONTH) != 1) { while (from.get(Calendar.DAY_OF_MONTH) != 1) { from.setTimeInMillis(from.getTimeInMillis() + ONE_DAY); } setMidnight(from); } // Auf das Ende des Vorherigen Monats setzen  GregorianCalendar temp = new GregorianCalendar(); temp.setTimeInMillis(to.getTimeInMillis() + ONE_DAY); if (temp.get(Calendar.MONTH) == to.get(Calendar.MONTH)) { while (to.get(Calendar.MONTH) == temp.get(Calendar.MONTH)) { to.setTimeInMillis(to.getTimeInMillis() - ONE_DAY); } set2359(to); } } /** * Gibt die Anzahl der Monate zurck, dabei wird aufgerundet * * @return anzahl der angefangenen Monate, also die Monate, die einen Anteil * an diesem Zeitraum haben */publicint getDurationInMonth() { int ergebnis = 0; int startmonat = from.get(GregorianCalendar.MONTH); int startjahr = from.get(GregorianCalendar.YEAR); int endmonat = to.get(GregorianCalendar.MONTH); int endjahr = to.get(GregorianCalendar.YEAR); int start = startjahr * 12 + startmonat; int ende = endjahr * 12 + endmonat; ergebnis = ende - start + 1; return ergebnis; } /** * gibt von_bis-Objekte zurck, die die an diesem Zeitraum beteiligten * Monate enthalten * * @return */public ArrayList<TimePeriod> getMonth() { ArrayList<TimePeriod> ergebnis = new ArrayList<TimePeriod>(); int anzahl = getDurationInMonth(); int startmonat = from.get(GregorianCalendar.MONTH); int startjahr = from.get(GregorianCalendar.YEAR); for (int i = 0; i < anzahl; i++) { TimePeriod temp = new TimePeriod(); temp.setMidnight(temp.from); // temp.set2359(temp.bis);  temp.from.set(Calendar.MONTH, startmonat); temp.from.set(Calendar.YEAR, startjahr); temp.from.set(Calendar.DAY_OF_MONTH, 1); temp.to.set(Calendar.MONTH, startmonat); temp.to.set(Calendar.YEAR, startjahr); temp.to.set(Calendar.DAY_OF_MONTH, 28); // wann der letzte tag im monat ist ist unklar, also solange weiter // gehen, // bis der neue monat anfngt, dann einen tag zurck while (temp.to.get(Calendar.MONTH) == temp.from.get(Calendar.MONTH)) temp.to.setTimeInMillis(temp.to.getTimeInMillis() + ONE_DAY); // Jetzt wieder einen tag zurck  temp.to.setTimeInMillis(temp.to.getTimeInMillis() - ONE_DAY); temp.set2359(temp.to); // Fertig! den Monat in die ArrayListe einfgen  ergebnis.add(temp); // weiter zum nchsten Monat  startmonat++; if (startmonat == 12) { startmonat = 0; startjahr++; } } return ergebnis; } /** * Gibt zurck, wie oft der bergebene Wochentag im Zeitraum vorkommt. * * @param wochentag * Sonntag = 1 * @return */publicint getNumberOfWeekdays(int wochentag) { int ergebnis = 0; GregorianCalendar temp = new GregorianCalendar(); temp.setTime(from.getTime()); // Schleife ber alle Tage int aktuellerTag; while (temp.before(to)) { aktuellerTag = temp.get(Calendar.DAY_OF_WEEK); if (aktuellerTag == wochentag) ergebnis++; temp.setTimeInMillis(temp.getTimeInMillis() + ONE_DAY); } return ergebnis; } } 








Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Format
4.Time Formatter
5.Returns time string
6.Returns the given date with the time values cleared
7.Convert the time to the midnight of the currently set date
8.Compare both times and dates
9.Tells you if the date part of a datetime is in a certain time range
10.Returns the given date with time set to the end of the day
11.Convert milliseconds to readable string
12.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
13.Returns a formatted String from time
14.Time library
15.Elapsed time in hours/minutes/seconds
16.Sets the time on the same day to 00:00:00.000
17.Determines if given times span at least an entire day
18.Converts a given time in milliseconds into a XMLGregorianCalendar object.
19.Time Distance
20.Time Formatter
21.Format time
22.A utility class for representing a span of time.
23.GmtCalendar is a useful class for working with times that are based using GMT time.
24.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
25.SimpleTimer enables bounded and unbounded waits.
26.Takes a time in milliseconds and returns an hours, minutes and seconds representation.
27.Format time in milliseconds
close