Java Time Different getDiffTime(String strDate, int idx)

Here you can find the source of getDiffTime(String strDate, int idx)

Description

Returns the difference portion of a date string.

License

Open Source License

Parameter

Parameter Description
strDate Date String.
idx Index of the +/- character.

Exception

Parameter Description
ParseException if <code>strDate</code> is in an invalid format.

Declaration

private static int[] getDiffTime(String strDate, int idx) throws ParseException 

Method Source Code

//package com.java2s;
/**/*from   ww  w  . j  ava  2  s.co  m*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: DateUtils.java,v 1.2 2008-06-25 05:53:00 qcheng Exp $
 *
 */

import java.text.ParseException;

public class Main {
    /**
     * Returns the difference portion of a date string. Array of integer with
     * the first element defining the hour difference; and second element
     * defining the minute difference
     * 
     * @param strDate Date String.
     * @param idx Index of the +/- character.
     * @returns the difference portion of a date string.
     * @throws ParseException if <code>strDate</code> is in an invalid format.
     */
    private static int[] getDiffTime(String strDate, int idx) throws ParseException {
        // discard the plus/minus char and trailing z char.
        String strDiff = strDate.substring(idx + 1, strDate.length() - 1);
        int[] diffArray = new int[2];
        int colonIdx = strDiff.indexOf(':');

        if (colonIdx == -1) {
            throw new ParseException("Invalid Date Format", 0);
        }

        try {
            diffArray[0] = Integer.parseInt(strDiff.substring(0, colonIdx));
            diffArray[1] = Integer.parseInt(strDiff.substring(colonIdx + 1));
        } catch (NumberFormatException nfe) {
            throw new ParseException("Invalid Date Format", 0);
        }

        return diffArray;
    }
}

Related

  1. diffTime(String sTime, String fTime, String Dateformat1)
  2. getDiffDateTime(int diff)
  3. getDifferent(final Date timeFrom, final Date timeTo)
  4. getDiffTime(Date beforeTime, Date afterTime)
  5. getDiffTime(long startTime)
  6. getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)
  7. getTimeDiff(String start, String end, String format, int timeDiff)
  8. getTimeDiffMin(String format, String startDate, String endDate)
  9. isDateDiff_2(String pattern, String time2, String time1)