Here you can find the source of createDate(String strDate, int[] timeDiff, boolean plusDiff)
Parameter | Description |
---|---|
strDate | String representation of a date. |
timeDiff | Time differences |
plusDiff | Time differences (plus/minus). true indicates do a plus to the computed date. Ignore this value if <code>timeDiff</code> is null. |
private static Date createDate(String strDate, int[] timeDiff, boolean plusDiff) throws ParseException
//package com.java2s; /**//from w ww.ja v a 2 s .c om * 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; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; public class Main { private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC"); /** * Returns a date with a string of yyyy-MM-ssThh:mm:ss or yyyy-MM-ssThh:mm. * * @param strDate String representation of a date. * @param timeDiff Time differences * @param plusDiff Time differences (plus/minus). true indicates do a plus * to the computed date. Ignore this value if <code>timeDiff</code> * is null. */ private static Date createDate(String strDate, int[] timeDiff, boolean plusDiff) throws ParseException { try { int year = Integer.parseInt(strDate.substring(0, 4)); if (strDate.charAt(4) != '-') { throw new ParseException("Invalid Date Format", 0); } int month = Integer.parseInt(strDate.substring(5, 7)) - 1; if (strDate.charAt(7) != '-') { throw new ParseException("Invalid Date Format", 0); } int day = Integer.parseInt(strDate.substring(8, 10)); if (strDate.charAt(10) != 'T') { throw new ParseException("Invalid Date Format", 0); } int hour = Integer.parseInt(strDate.substring(11, 13)); if (strDate.charAt(13) != ':') { throw new ParseException("Invalid Date Format", 0); } int minute = Integer.parseInt(strDate.substring(14, 16)); int second = 0; if (strDate.length() > 17) { if (strDate.charAt(16) != ':') { throw new ParseException("Invalid Date Format", 0); } second = Integer.parseInt(strDate.substring(17, 19)); } GregorianCalendar cal = new GregorianCalendar(year, month, day, hour, minute, second); cal.setTimeZone(UTC_TIME_ZONE); if (timeDiff != null) { int hourDiff = (plusDiff) ? timeDiff[0] : (-1 * timeDiff[0]); int minuteDiff = (plusDiff) ? timeDiff[1] : (-1 * timeDiff[1]); cal.add(Calendar.HOUR, hourDiff); cal.add(Calendar.MINUTE, minuteDiff); } return cal.getTime(); } catch (NumberFormatException nfe) { throw new ParseException("Invalid Date Format", 0); } } }