Java TimeZone getDateValue(TimeZone zone, Long dateBoxValue, Long timeBoxValue)

Here you can find the source of getDateValue(TimeZone zone, Long dateBoxValue, Long timeBoxValue)

Description

Returns the Date for the values of the UTCDateBox and UTCTimeBox which were edited in the specified TimeZone .

License

Apache License

Parameter

Parameter Description
zone The TimeZone in which the Date was edited.
dateBoxValue The value of the UTCDateBox control.
timeBoxValue The value of the UTCTimeBox control.

Return

The that has been selected from the controls or null if the supplied dateBoxValue is null. If the timeBoxValue is null, midnight is returned.

Declaration

public static final Date getDateValue(TimeZone zone, Long dateBoxValue,
        Long timeBoxValue) 

Method Source Code

//package com.java2s;
/*/*  www. j a v a  2s . c  om*/
 * Copyright 2010 Traction Software, Inc.
 * 
 * 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.
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class Main {
    /**
     * Returns the {@link Date} for the values of the UTCDateBox and
     * UTCTimeBox which were edited in the specified {@link TimeZone}.
     * 
     * @param zone
     *            The {@link TimeZone} in which the Date was edited.
     * @param dateBoxValue
     *            The value of the {@link UTCDateBox} control.
     * @param timeBoxValue
     *            The value of the {@link UTCTimeBox} control.
     * @return The {@link Date} that has been selected from the
     *         controls or null if the supplied dateBoxValue is null.
     *         If the timeBoxValue is null, midnight is returned.
     */
    public static final Date getDateValue(TimeZone zone, Long dateBoxValue,
            Long timeBoxValue) {

        if (dateBoxValue == null)
            return null;

        Calendar gmt = GregorianCalendar.getInstance(TimeZone
                .getTimeZone("GMT"));
        gmt.setTimeInMillis(dateBoxValue.longValue());

        Calendar cal = GregorianCalendar.getInstance(zone);
        cal.set(gmt.get(Calendar.YEAR), gmt.get(Calendar.MONTH),
                gmt.get(Calendar.DAY_OF_MONTH));

        int hours, minutes, extraMillis;

        if (timeBoxValue != null) {
            // figure out how many hours and minutes to add to
            // midnight in the specified time zone.
            long localTimeInDay = timeBoxValue.longValue();

            // figure out if there are extra millis in the value
            // (there shoudn't be since the time box control doesn't
            // typically render millis)
            extraMillis = (int) (localTimeInDay % (60 * 1000));

            // trim off the seconds
            localTimeInDay -= extraMillis;

            minutes = (int) ((localTimeInDay / 60 / 1000) % 60);

            // trim off the minutes
            localTimeInDay -= minutes;

            hours = (int) (localTimeInDay / 60 / 60 / 1000);
        } else {
            // midnight
            hours = 0;
            minutes = 0;
            extraMillis = 0;
        }

        cal.set(Calendar.HOUR_OF_DAY, hours);
        cal.set(Calendar.MINUTE, minutes);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return new Date(cal.getTimeInMillis() + extraMillis);
    }
}

Related

  1. extractTimeZone(String strdate, Date thedate)
  2. getDateAtTimeZone(Date when, TimeZone from, TimeZone to)
  3. getDateAttributes(long millisec, TimeZone tz, boolean showNumber)
  4. getDateBoxValue(TimeZone zone, Date date)
  5. getDateWithoutTimeZone(Date date)
  6. getExcelSerialDayNumber(Date date, Locale locale, TimeZone timeZone)
  7. getLocalTime( java.util.TimeZone localTimeZone, java.util.Date timestamp, java.util.TimeZone sourceTimeZone)
  8. getTimezoneDate(String stateCode)