org.wicketstuff.ddcalendar.CalendarWeek.java Source code

Java tutorial

Introduction

Here is the source code for org.wicketstuff.ddcalendar.CalendarWeek.java

Source

/*
 * Copyright 2009 Michael Wrtinger (mwuertinger@users.sourceforge.net)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wicketstuff.ddcalendar;

import java.io.Serializable;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;

public class CalendarWeek implements Serializable {
    private static final long serialVersionUID = 1L;
    private int year;
    private int week;

    public CalendarWeek() {
        DateMidnight now = new DateMidnight();
        this.year = now.getYear();
        this.week = now.getWeekOfWeekyear();
    }

    public CalendarWeek(int year, int week) {
        this.year = year + ((week - 1) / 52);
        this.week = 1 + (week - 1) % 52;
    }

    public int getYear() {
        return year;
    }

    public int getWeek() {
        return week;
    }

    public CalendarWeek getNextWeek() {
        return new CalendarWeek(year, week + 1);
    }

    public CalendarWeek getPreviousWeek() {
        return new CalendarWeek(year, week - 1);
    }

    public DateMidnight getFirstDay() {
        DateMidnight date = new DateMidnight(year, 1, 1);
        date = date.withWeekOfWeekyear(week);
        date = date.withDayOfWeek(1);
        return date;
    }

    public DateMidnight getLastDay() {
        DateMidnight date = new DateMidnight(year, 1, 1);
        date = date.withWeekOfWeekyear(week);
        date = date.withDayOfWeek(7);
        return date;
    }

    public boolean isInWeek(DateTime date) {
        return date.getWeekOfWeekyear() == week && date.getYear() == year;
    }
}