Java Calendar Time parseTime(String time, Calendar calendar)

Here you can find the source of parseTime(String time, Calendar calendar)

Description

Parses a time string in the format [Tt]HH:MM:SS(.[0-9]+)?

License

Apache License

Parameter

Parameter Description
time the time string that will be parsed
calendar the calendar object that will be updated with the parsed time

Declaration

private static long parseTime(String time, Calendar calendar) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.Calendar;

public class Main {
    private static final int NUMERIC_TIME_ZONE_LENGTH = 6;
    private static final int MIN_TIME_STRING_LENGTH = 7;

    /**/*from w  ww.jav a  2  s  .co m*/
     * Parses a time string in the format [Tt]HH:MM:SS(.[0-9]+)?
     * followed by an optional timezone which is [Zz] or [-+]HH:MM
     * 
     * @param time the time string that will be parsed
     * @param calendar the calendar object that will be updated with the parsed time
     * @return
     */
    private static long parseTime(String time, Calendar calendar) {
        if (time.length() < MIN_TIME_STRING_LENGTH) {
            throw new NumberFormatException();
        }

        long timeZoneOffset = 0;

        char first = time.charAt(0);
        // There should be a t separating the date and time and
        // it should be long enought to fit "THHMMSS".
        if (time.length() < MIN_TIME_STRING_LENGTH || (first != 'T' && first != 't')) {
            throw new NumberFormatException();
        }
        // everything after the 'T' or 't'
        time = time.substring(1);

        int value = Integer.parseInt(time.substring(0, 2));
        if (value > 23) {
            throw new NumberFormatException();
        }
        calendar.set(Calendar.HOUR_OF_DAY, value);
        if (time.charAt(2) == ':') {
            time = time.substring(3);
        } else {
            time = time.substring(2);
        }

        value = Integer.parseInt(time.substring(0, 2));
        if (value > 59) {
            throw new NumberFormatException();
        }
        calendar.set(Calendar.MINUTE, value);
        if (time.charAt(2) == ':') {
            time = time.substring(3);
        } else {
            time = time.substring(2);
        }

        value = Integer.parseInt(time.substring(0, 2));
        if (value > 59) {
            throw new NumberFormatException();
        }
        calendar.set(Calendar.SECOND, value);
        time = time.substring(2);

        calendar.set(Calendar.MILLISECOND, 0);

        if (time.length() > 0) {
            try {
                // If the next character is a '.' we have a miliseconds specified in
                // the string.
                int currentStringPos = 0;
                if (time.charAt(currentStringPos) == '.') {
                    ++currentStringPos;
                    // Next character must be a digit
                    if (!Character.isDigit(time.charAt(currentStringPos))) {
                        throw new NumberFormatException();
                    }
                    int multiplier = 100;
                    int milliseconds = Character.digit(time.charAt(currentStringPos), 10) * multiplier;

                    // There may be 0 or more more digits to process
                    // Note if there are more than 3 digits in total we will be ignoring
                    // the fractions of a millisecond sepecified.
                    ++currentStringPos;
                    while (currentStringPos < time.length() && Character.isDigit(time.charAt(currentStringPos))) {
                        multiplier /= 10;
                        milliseconds += Character.digit(time.charAt(currentStringPos), 10) * multiplier;
                        currentStringPos++;
                    }
                    calendar.set(Calendar.MILLISECOND, milliseconds);
                }
                // If we still have characters to process this should
                // be timezone information. Which is either a 'Z' or
                // +HH:MM or -HH:MM
                if (currentStringPos < time.length()) {
                    String timezone = time.substring(currentStringPos);

                    // If its a string of 1 character which is a Z we are
                    // using Universal time, so no change nessesary.
                    if ((timezone.charAt(0) == 'Z' || timezone.charAt(0) == 'z') && timezone.length() == 1) {
                        // Nothing to do here.

                        // Check to see if it might be of the form [+-]HH:MM
                    } else if (timezone.length() == NUMERIC_TIME_ZONE_LENGTH
                            && (timezone.charAt(0) == '+' || timezone.charAt(0) == '-')
                            && timezone.charAt(3) == ':') {
                        int hours = Integer.parseInt(timezone.substring(1, 3));
                        int minutes = Integer.parseInt(timezone.substring(4, 6));
                        timeZoneOffset = ((hours * 60) + minutes) * 60000;
                        if (timezone.charAt(0) == '+') {
                            timeZoneOffset = -timeZoneOffset;
                        }
                    } else {
                        // We have spare characters that aren't a valid timezone,
                        // so its an invalid date.
                        throw new NumberFormatException();
                    }
                }
            } catch (IndexOutOfBoundsException e) {
                throw new NumberFormatException();
            }
        }
        return timeZoneOffset;
    }
}

Related

  1. mergeDateTime(Calendar date, Calendar time)
  2. newCalendar(final long time)
  3. newCalendarInstance(TimeZone timeZone)
  4. normalizeLocalTime(final Calendar cal)
  5. parseTime(Calendar calendar, String time_string)
  6. removeTime(Calendar c)
  7. removeTime(Calendar cal)
  8. removeTime(Calendar calendar)
  9. removeTime(final Calendar date)