Java Date ISO Parse toCalendar(final String iso8601string)

Here you can find the source of toCalendar(final String iso8601string)

Description

Transform ISO 8601 string to Calendar.

License

Open Source License

Declaration

public static Calendar toCalendar(final String iso8601string) throws ParseException 

Method Source Code

//package com.java2s;
/**//from  w ww. ja  va2  s .  co  m
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**
     * Transform ISO 8601 string to Calendar.
     */
    public static Calendar toCalendar(final String iso8601string) throws ParseException {
        Calendar calendar = GregorianCalendar.getInstance();
        String s = iso8601string.replace("Z", "+00:00");
        try {
            s = s.substring(0, 22) + s.substring(23);
        } catch (IndexOutOfBoundsException e) {
            throw new ParseException("Invalid length", 0);
        }
        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse(s);
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. parseIsoString(String time)
  2. parseTimestampIso8601(String timestamp)
  3. strongCheckIso8601Date(String date)
  4. time2StringISO(Date date)
  5. timestampAsIsoString(long timestamp)
  6. toDate(String isoDate)
  7. toIsoString(Calendar value)
  8. toIsoString(Date date)
  9. toStringForComparison(Date date)