Java Timestamp Field getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration)

Here you can find the source of getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration)

Description

Get expiration timestamp from start date, period type and duration.

License

Open Source License

Parameter

Parameter Description
tsStartDate - start date of period counting
iPeriodType - one of the period type constant TIMING_XXX
iPeriodDuration - period duration, number of time units specified by period type

Return

Timestamp - date of period expiration or null if any problem

Declaration

public static Timestamp getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration) 

Method Source Code


//package com.java2s;
/*/*w ww .j a v  a2s.  c o  m*/
 * Copyright (C) 2003 - 2013 OpenSubsystems.com/net/org and its owners. All rights reserved.
 * 
 * This file is part of OpenSubsystems.
 *
 * OpenSubsystems is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

import java.sql.Timestamp;

import java.util.Calendar;

public class Main {
    /**
     * Constant for timing type
     */
    public static final int TIMING_NEVER = 0;
    /**
     * Constant for timing type
     */
    public static final int TIMING_MINUTES = 1;
    /**
     * Constant for timing type
     */
    public static final int TIMING_HOURS = 2;
    /**
     * Constant for timing type
     */
    public static final int TIMING_DAYS = 3;
    /**
     * Constant for timing type
     */
    public static final int TIMING_WEEKS = 4;
    /**
     * Constant for timing type
     */
    public static final int TIMING_MONTHS = 5;
    /**
     * Constant for timing type
     */
    public static final int TIMING_YEARS = 6;
    /**
     * Constant for timing type
     */
    public static final int TIMING_NONE = 7;

    /**
     * Get expiration timestamp from start date, period type and duration. For 
     * example if the start date is now, period type is hour and duration is 2
     * then the result will be timestamp representing now + 2 hours.  
     * 
     * @param tsStartDate - start date of period counting
     * @param iPeriodType - one of the period type constant TIMING_XXX 
     * @param iPeriodDuration - period duration, number of time units specified 
     *                          by period type
     * @return Timestamp - date of period expiration or null if any problem
     */
    public static Timestamp getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration) {
        Timestamp tsReturn = null;
        Calendar calHelp;
        if (tsStartDate != null && iPeriodDuration > 0 && iPeriodType > TIMING_NEVER && iPeriodType < TIMING_NONE) {
            calHelp = Calendar.getInstance();
            calHelp.setTime(tsStartDate);

            switch (iPeriodType) {
            case (TIMING_MINUTES): {
                calHelp.add(Calendar.MINUTE, iPeriodDuration);
                break;
            }
            case (TIMING_HOURS): {
                calHelp.add(Calendar.HOUR, iPeriodDuration);
                break;
            }
            case (TIMING_DAYS): {
                calHelp.add(Calendar.DATE, iPeriodDuration);
                break;
            }
            case (TIMING_WEEKS): {
                calHelp.add(Calendar.WEEK_OF_YEAR, iPeriodDuration);
                break;
            }
            case (TIMING_MONTHS): {
                calHelp.add(Calendar.MONTH, iPeriodDuration);
                break;
            }
            case (TIMING_YEARS): {
                calHelp.add(Calendar.YEAR, iPeriodDuration);
                break;
            }
            default: {
                assert false : "Not supported Timing type " + iPeriodType;
            }
            }
            tsReturn = new Timestamp(calHelp.getTimeInMillis());
        }

        return tsReturn;
    }
}

Related

  1. getNowTimestamp()
  2. getNowTimeStamp()
  3. getNowTimeStamp()
  4. getOffsetDaysTime(Timestamp sysDate, int offsetDays)
  5. getPerformanceTime(Timestamp startTime)
  6. getPresentTimeStamp()
  7. getRandomTimeBetweenTwoDates(Timestamp beginDate, Map context)
  8. getRandTimestamp(Random r)
  9. getRealCalendar(Timestamp time)