Java Date ISO Parse normalizeToISO8601(String sDate, TimeZone tz)

Here you can find the source of normalizeToISO8601(String sDate, TimeZone tz)

Description

Convert the given sDate in iso 8601 format.

License

Open Source License

Parameter

Parameter Description
sDate String

Return

the sDate in iso 8601 format

Declaration

public static String normalizeToISO8601(String sDate, TimeZone tz) 

Method Source Code

//package com.java2s;
/*//from  ww w.  j  a  v a  2s . c  o m
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2005 - 2007 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    public final static String PATTERN_UTC = "yyyyMMdd'T'HHmmss'Z'";
    public final static String PATTERN_UTC_WSEP = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    public final static TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC");

    /**
     * Convert the given sDate in iso 8601 format.
     * <P>The formats accepted are:
     * <ul>
     * <li>yyyyMMdd'T'HHmmss'Z' (if the date is in this format and the given
     * timezone isn't null and the date doesn't end with 000000Z, the date is localized with
     * the given timezone). We have to check also if the date end with 000000Z because
     * some phones (as Nokia 7650) sends the birthday with the 'Z' but isn't localized</li>
     * <li>yyyy-MM-dd'T'HH:mm:ss'Z' (if the date is in this format and the given
     * timezone isn't null and the date doesn't end with 00:00:00Z, the date is localized with
     * the given timezone).</li>
     * <li>yyyy-MM-dd</li>
     * <li>yyyy/MM/dd</li>
     * <li>yyyyMMdd</li>
     * <li>all formats starts with the previous format (i.e. yyyy-MM-ddTHH:mm:ss.sss)</li>
     * </ul>
     *
     * @param sDate String
     * @return the sDate in iso 8601 format
     */
    public static String normalizeToISO8601(String sDate, TimeZone tz) {

        if (sDate == null || sDate.equals("")) {
            return sDate;
        }

        if (tz != null) {
            //
            // Try to apply the timezone
            //
            SimpleDateFormat utcFormatter = new SimpleDateFormat(PATTERN_UTC);
            utcFormatter.setTimeZone(TIMEZONE_UTC);
            Date date = null;
            try {
                date = utcFormatter.parse(sDate);
                if (!sDate.endsWith("000000Z")) {
                    utcFormatter.setTimeZone(tz);
                    sDate = utcFormatter.format(date);
                }
            } catch (ParseException ex) {
                //
                // Ignore this error. The date isn't in this format.
                //
                // Try with yyyy-MM-dd'T'HH:mm:ss'Z'
                utcFormatter.applyPattern(PATTERN_UTC_WSEP);
                try {
                    date = utcFormatter.parse(sDate);
                    if (!sDate.endsWith("00:00:00Z")) {
                        utcFormatter.setTimeZone(tz);
                        sDate = utcFormatter.format(date);
                    }

                } catch (Exception e) {
                    //
                    // Ignore this error. The date isn't in this format.
                    //
                }
            }
        }
        int year = -1;
        int month = -1;
        int day = -1;
        String tmp = null;
        int last = 0;

        //
        // The first four digits are the year
        //
        tmp = sDate.substring(0, 4);
        year = Integer.parseInt(tmp);

        //
        // Read the month
        //
        char c = sDate.charAt(4);
        if (c == '/' || c == '-') {
            tmp = sDate.substring(5, 7);
            last = 7;
        } else {
            tmp = sDate.substring(4, 6);
            last = 6;
        }
        month = Integer.parseInt(tmp);

        //
        // Read the day
        //
        c = sDate.charAt(last);
        if (c == '/' || c == '-') {
            tmp = sDate.substring(last + 1, last + 3);
        } else {
            tmp = sDate.substring(last, last + 2);
        }
        day = Integer.parseInt(tmp);

        StringBuffer isoDate = new StringBuffer(10);
        isoDate.append(year).append("-");

        if (month < 10) {
            isoDate.append("0");
        }
        isoDate.append(month).append("-");

        if (day < 10) {
            isoDate.append("0");
        }
        isoDate.append(day);
        return isoDate.toString();
    }
}

Related

  1. ISO8601ToSeconds(String iso8601)
  2. isoDateStringToDate(String dateString)
  3. isOneOf(String value, String... values)
  4. ISOToJulianDate(String dateObs)
  5. isValidISO8601(String time)
  6. parseAwsFlavouredISO8601Date(String dateString)
  7. parseDate(String iso8061StrDateTime)
  8. parseDateISO(String date)
  9. parseDateISO(String value)