Java Timestamp adjustTimestamp(Timestamp timestamp, String timezone, int gmtOffset)

Here you can find the source of adjustTimestamp(Timestamp timestamp, String timezone, int gmtOffset)

Description

Adjust java.sql.Timestamp for the specified timezone change or gmt offset (using one overrides the other).

License

Apache License

Parameter

Parameter Description
timestamp - base timestamp
timezone - plus/minus hours:minutes to adjust
gmtOffset - plus/minus hours to adjust

Return

Timestamp

Declaration

public static Timestamp adjustTimestamp(Timestamp timestamp,
        String timezone, int gmtOffset) 

Method Source Code

//package com.java2s;
/*/*from  www .  j a  v a  2  s.  c o  m*/
 // Licensed to DynamoBI Corporation (DynamoBI) under one
 // or more contributor license agreements.  See the NOTICE file
 // distributed with this work for additional information
 // regarding copyright ownership.  DynamoBI licenses this file
 // to you 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.
 */

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String MINUTE = "MINUTE";

    /**
     * Adjust java.sql.Timestamp for the specified
     * timezone change or gmt offset (using one overrides the other).
     *
     * @param timestamp - base timestamp
     * @param timezone - plus/minus hours:minutes to adjust
     * @param gmtOffset - plus/minus hours to adjust
     * @return Timestamp
     */
    public static Timestamp adjustTimestamp(Timestamp timestamp,
            String timezone, int gmtOffset) {
        Timestamp ts = null;

        if (timezone != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(timestamp.getTime());

            Pattern p = Pattern
                    .compile("([\\+\\-]?)([\\d]{1,2})[:]?([\\d]{1,2})?");
            Matcher m = p.matcher(timezone);

            if (m.find()) {
                String hh = m.group(2);

                String sign = m.group(1);
                int hours = Integer.valueOf(hh);
                int minutes = 0;

                if (m.group(3) != null) {
                    String mm = m.group(3);
                    minutes = Integer.valueOf(mm);
                }

                gmtOffset = (hours * 60 + minutes);

                if (sign.compareTo("-") == 0) {
                    gmtOffset *= (-1);
                }
            } else {
                // The gmtOffset was passed in
                // To support half-hour offsets, convert to minutes.

                gmtOffset *= 60;
            }

            cal.add(Calendar.MINUTE, gmtOffset);

            ts = new Timestamp(cal.getTimeInMillis());
        }

        return (ts != null) ? ts : timestamp;
    }
}

Related

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