# Daten der gesamten Woche anzeigen



## TimSkyp (5. Jan 2011)

Hallo,

Ich bin grad dabei einen Organizer zu erstellen, der eine Wochenansicht hat und in dem man dann auch Termine eintragen kann.
Ich weiß aber nicht wie ich es machen soll, dass das richtige Datum für jeden Tag der Woche angezeigt wird. 
Ich hab bis jetzt nur das aktuelle Datum von heute anzeigen können (aus das bin ich ziemlich schnell gekommen).

Ich würde mich sehr freuen wenn mir wer einen Tipp/Hinweis geben kann wie man die Daten der gesamten aktuellen Woche anzeigen kann.


----------



## ARadauer (5. Jan 2011)

Schau dir mal die Klasse Calendar (Java Platform SE 6) bzw GregorianCalendar (Java Platform SE 6) an


----------



## André Uhres (6. Jan 2011)

Hallo TimSkyp,

herzlich willkommen in unserem Java Forum!

Die Methode "getDisplayDays" der Klasse "WeekModel" gibt eine Liste der Daten der gesamten aktuellen Woche zurück. 

Die Klasse enthält auch eine "main" Methode. Dort wird an einem Beispiel gezeigt, wie wir "WeekModel" benutzen können:


```
/*
 * WeekModel.java
 *
 * This class is used to hold the list of days
 * corresponding to the selected week
 */

import java.text.*;
import java.util.*;

/**
 *
 * @author André Uhres
 */
public class WeekModel {

    //The list of days corresponding to the selected week:
    private List<Day> daysList;
    //This is always the actual calendar date:
    private Date currentDate;
    //The Calendar corresponding to the selected week:
    private Calendar selectedWeek;
    //Working fields:
    private Calendar temp;
    private int daysBefore;

    public WeekModel() {
        //The selected week is set
        //initially to the current calendar week:
        selectedWeek = DateSupport.dateOnlyCalendar();
        currentDate = selectedWeek.getTime();//This ia always the actual calendar date
        temp = DateSupport.dateOnlyCalendar();
        //create the list of days corresponding to the selected week:
        daysList = createDaysList();
    }

    /**
     * @return the Calendar after the next week is selected in the model
     */
    public Calendar nextWeek() {
        selectedWeek.add(Calendar.WEEK_OF_YEAR, 1);
        daysList = createDaysList();
        return selectedWeek;
    }

    /**
     * @return the Calendar after the previous week is selected in the model
     */
    public Calendar previousWeek() {
        selectedWeek.add(Calendar.WEEK_OF_YEAR, -1);
        daysList = createDaysList();
        return selectedWeek;
    }

    /**
     * @return void this week is selected in the model
     */
    public void setWeek(final Date date) {
        selectedWeek.setTime(DateSupport.convertToDateOnly(date));
        daysList = createDaysList();
    }

    /**
     * @return the list of days to be displayed
     */
    public List<Day> getDisplayDays() {
        return daysList;
    }

    //The days list is created each time a new week is selected:
    private List<Day> createDaysList() {
        List<Day> list = new ArrayList<Day>();
        fillDaysCurrent(list);
        return list;
    }

    //Fills the days list with the days of selected week:
    private void fillDaysCurrent(final List<Day> list) {
        temp.setTime(selectedWeek.getTime());
        daysBefore = temp.get(Calendar.DAY_OF_WEEK) - temp.getFirstDayOfWeek();
        if (daysBefore < 0) {//if negative result (DAY_OF_WEEK is before firstDayOfWeek)
            daysBefore += 7;//add one week
        }
        temp.add(Calendar.DATE, -daysBefore);
        for (int i = 0; i < 7; i++) {
            list.add(new Day(temp, currentDate));
            temp.add(Calendar.DATE, 1);
        }
    }

    public void setCurrentDate(final Date currentDate) {
        this.currentDate = currentDate;
    }

    public Calendar getSelectedWeek() {
        return selectedWeek;
    }

    public static void main(final String... args) {
        WeekModel model = new WeekModel();
        List<Day> displayDays = null;

        System.out.println("previous week:");
        model.previousWeek();
        displayDays = model.getDisplayDays();
        for (Day day : displayDays) {
            System.out.println(day + (day.isCurrent() ? " current day" : ""));
        }

        System.out.println("current week:");
        model.nextWeek();
        displayDays = model.getDisplayDays();
        for (Day day : displayDays) {
            System.out.println(day + (day.isCurrent() ? " current day" : ""));
        }
        System.out.println("next week:");
        model.nextWeek();
        displayDays = model.getDisplayDays();
        for (Day day : displayDays) {
            System.out.println(day + (day.isCurrent() ? " current day" : ""));
        }
    }
}

class Day {

    //The individual day number to be displayed
    private int day;
    //Indicates if this day is the actual calendar day
    private boolean current;
    //The Date object corresponding to this day:
    private Date date;
    private final Calendar calendar;
    public static final SimpleDateFormat dfmt = new SimpleDateFormat("E dd/MM/yyyy");

    public Day() {
        this(DateSupport.dateOnlyCalendar(), DateSupport.convertToDateOnly(new Date()));
    }

    public Day(final Calendar calendar, final Date currentDate) {
        this.calendar = DateSupport.dateOnlyCalendar();
        this.calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        this.calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
        this.calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
        day = calendar.get(Calendar.DATE);//The individual day number
        current = calendar.getTime().equals(currentDate);//if this day is the actual calendar day
        date = calendar.getTime();//The Date object corresponding to this day
    }

    /**
     * @return true if this day is the actual calendar day
     */
    public boolean isCurrent() {
        return current;
    }

    /**
     * @return the Date object corresponding to this day
     */
    public Date getDate() {
        return date;
    }

    public Calendar getCalendar() {
        return calendar;
    }

    public int getDay() {
        return day;
    }

    public void setCurrent(final boolean current) {
        this.current = current;
    }

    public void setDate(final Date date) {
        this.date = date;
    }

    public void setDay(final int day) {
        this.day = day;
    }

    /**
     * @return the day string to be displayed
     */
    @Override
    public String toString() {
        return Day.dfmt.format(date);
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Day other = (Day) obj;
        if (this.date != other.date && (this.date == null || !this.date.equals(other.date))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.date != null ? this.date.hashCode() : 0);
        return hash;
    }
}

class DateSupport {

    /**
     * @return a Calendar instance where only the fields YEAR, MONTH and DATE are set
     */
    public static Calendar dateOnlyCalendar() {
        Calendar now = Calendar.getInstance();
        Calendar dateOnly = Calendar.getInstance();
        //We set only YEAR, MONTH and DATE fields (no hour, minute, second, ...).
        //This allows us to see if two dates are equal independantly of other fields.
        dateOnly.clear();
        dateOnly.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE));
        return dateOnly;
    }

    /**
     * Converts a date into a date with only YEAR, MONTH and DATE fields set
     * (no hour, minute second,...)
     */
    public static Date convertToDateOnly(final Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar dateOnly = dateOnlyCalendar();
        dateOnly.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
        return dateOnly.getTime();
    }
}
```

Gruß,
André


----------

