Java TimeZone Get getTime(final String time, final TimeZone tz)

Here you can find the source of getTime(final String time, final TimeZone tz)

Description

get Time

License

Open Source License

Declaration

public static Calendar getTime(final String time, final TimeZone tz)
            throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 Lifeform Software.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from www.  j a  v a 2  s. co  m*/
 *     Ernan Hughes - initial API and implementation
 *******************************************************************************/

import java.util.Calendar;

import java.util.StringTokenizer;
import java.util.TimeZone;

public class Main {
    public static Calendar getTime(final String time, final TimeZone tz)
            throws Exception {
        int hours, minutes;
        StringTokenizer st = new StringTokenizer(time, ":");
        int tokens = st.countTokens();
        if (tokens != 2) {
            String msg = "Time " + time
                    + " does not conform to the HH:MM format.";
            throw new Exception(msg);
        }

        String hourToken = st.nextToken();
        try {
            hours = Integer.parseInt(hourToken);
        } catch (NumberFormatException nfe) {
            String msg = hourToken + " in " + time
                    + " can not be parsed as hours.";
            throw new Exception(msg);
        }

        String minuteToken = st.nextToken();
        try {
            minutes = Integer.parseInt(minuteToken);
        } catch (NumberFormatException nfe) {
            String msg = minuteToken + " in " + time
                    + " can not be parsed as minutes.";
            throw new Exception(msg);
        }

        if (hours < 0 || hours > 23) {
            String msg = "Specified hours: " + hours
                    + ". Number of hours must be in the [0..23] range.";
            throw new Exception(msg);
        }

        if (minutes < 0 || minutes > 59) {
            String msg = "Specified minutes: " + minutes
                    + ". Number of minutes must be in the [0..59] range.";
            throw new Exception(msg);
        }

        Calendar period = Calendar.getInstance(tz);
        period.set(Calendar.HOUR_OF_DAY, hours);
        period.set(Calendar.MINUTE, minutes);
        // set seconds explicitly, otherwise they will be carried from the
        // current time
        period.set(Calendar.SECOND, 0);

        return period;
    }
}

Related

  1. getPossibleUserTimezoneList(int userOffsetInHours)
  2. getRawOffset(String timeZoneStr)
  3. getRawOffsetDifference(TimeZone from, TimeZone to)
  4. getServerTimezoneOffset()
  5. getTime(boolean lenient, TimeZone tz, int year, int month, int day, int hour, int minute, int second, boolean setMillis, int nano)
  6. getTime(TimeZone tz, int year, int month, int day, int hour, int minute, int second)
  7. getTime(TimeZone zone)
  8. getTimeAsPerTimeZone(String time, String timeZOne)
  9. getTimeInCurrentDay(long now, TimeZone tz, int hour, int minute, int second, int millisecond)