models.JWeek.java Source code

Java tutorial

Introduction

Here is the source code for models.JWeek.java

Source

/*
 * Copyright (C) 2012 mPower Social
 *
 * 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 models;

import java.io.Serializable;
import java.util.*;

import org.joda.time.*;

import play.Logger;

/**
 * JiVitA Week.
 */
public class JWeek extends Date implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = -7048519540658872745L;

    /* 1st JiVitA week: 1 January, 2011
     * Week starts from Friday to Thursday
     * So, we have to start from 29 December, 2000 (Friday) for ease of calculation.
     *
     * To count the first Friday as "day one", we are calculating start day from
     * the day before it (28 December, 2000 (Thursday))
     */

    /** The Constant JiVitA epoch. */
    transient private final DateTime jivitaEpoch = new DateTime(2000, 12, 28, 0, 0, 0, 0);

    public JWeek() {
        this(new Date());
    }

    /**
     * Instantiates a new JiVitA week.
     *
     * @param i the integer
     */
    public JWeek(Integer i) {
        /* Adding 1 day to jivitaEpoch to start from 29 December, 2000 (Friday).
          * Subtracting 1 from jivitaWeek to get the first day of week.
          */
        DateTime date = jivitaEpoch.plusDays(1).plusWeeks(i - 1);
        this.setTime(date.getMillis());
    }

    /**
     * Instantiates a new JiVitA week.
     *
     * @param d the date
     */
    public JWeek(Date date) {
        this.setTime(date.getTime());
    }

    public JWeek(java.sql.Date date) {
        this.setTime(date.getTime());
    }

    public JWeek(DateTime dateTime) {
        this.setTime(dateTime.getMillis());
    }

    /**
      * Gets the JiVitA week.
      *
      * @return the JiVitA week
      */
    public int getJWeek() {
        Days days = Days.daysBetween(jivitaEpoch, new DateTime(this));
        double dDays = (double) days.getDays();
        double weeks = Math.ceil(dDays / 7.0d);
        return (int) weeks;
    }

    public DateTime getDateTime() {
        return new DateTime(this);
    }

    public String toString() {
        return "" + this.getJWeek();
    }

}