Java Timestamp adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity)

Here you can find the source of adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity)

Description

adjust Timestamp

License

Apache License

Declaration

public static Timestamp adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;

import java.util.Calendar;
import java.util.Date;

import java.util.Locale;
import java.util.TimeZone;

public class Main {
    /**//from   www  . jav a2 s . co  m
     * Perform date/time arithmetic on a Timestamp. This is the only accurate
     * way to perform date/time arithmetic across locales and time zones.
     *
     * @param stamp
     *            date/time to perform arithmetic on
     * @param adjType
     *            the adjustment type to perform. Use one of the
     *            java.util.Calendar fields.
     * @param adjQuantity
     *            the adjustment quantity.
     * @param timeZone
     * @param locale
     * @return adjusted Timestamp
     * @see java.util.Calendar
     */
    public static Timestamp adjustTimestamp(Timestamp stamp, int adjType, int adjQuantity, TimeZone timeZone,
            Locale locale) {
        Calendar tempCal = toCalendar(stamp, timeZone, locale);
        tempCal.add(adjType, adjQuantity);
        return new Timestamp(tempCal.getTimeInMillis());
    }

    public static Timestamp adjustTimestamp(Timestamp stamp, Integer adjType, Integer adjQuantity) {
        Calendar tempCal = toCalendar(stamp);
        tempCal.add(adjType, adjQuantity);
        return new Timestamp(tempCal.getTimeInMillis());
    }

    public static java.util.Calendar toCalendar(java.sql.Timestamp stamp) {
        Calendar cal = Calendar.getInstance();
        if (stamp != null) {
            cal.setTimeInMillis(stamp.getTime());
        }
        return cal;
    }

    /**
     * Returns a Calendar object initialized to the specified date/time, time
     * zone, and locale.
     *
     * @param date
     *            date/time to use
     * @param timeZone
     * @param locale
     * @return Calendar object
     * @see java.util.Calendar
     */
    public static Calendar toCalendar(Date date, TimeZone timeZone, Locale locale) {
        Calendar cal = Calendar.getInstance(timeZone, locale);
        if (date != null) {
            cal.setTime(date);
        }
        return cal;
    }
}

Related

  1. addMilli(Timestamp nowDate, int period)
  2. addMonth(java.sql.Timestamp src, int val)
  3. addMonths(Timestamp refDate, int nrOfMonthsToAdd)
  4. addTime(java.sql.Timestamp start, int year, int month, int day)
  5. adjustTimestamp(Timestamp stamp, int adjType, int adjQuantity, TimeZone timeZone, Locale locale)
  6. adjustTimestamp(Timestamp timestamp, String timezone, int gmtOffset)
  7. afterMinutes(Timestamp date, long min)
  8. argMapTimestamp(Map argMap, Map argMapNotUsed, String key)
  9. beforeTimestamp2Safety(Timestamp ts1, Timestamp ts2)