Java Date Format Change convertISO8610ToCalendar(String iso8601string)

Here you can find the source of convertISO8610ToCalendar(String iso8601string)

Description

Convert an ISO8601 UTC string to a Calendar object.

License

Open Source License

Parameter

Parameter Description
iso8601string ISO8601 UTC string

Return

Calendar object

Declaration

public static Calendar convertISO8610ToCalendar(String iso8601string) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /**//w w w  . j  a v  a  2  s . co m
     * Convert an ISO8601 UTC string to a Calendar object.
     * @param iso8601string ISO8601 UTC string
     * @return Calendar object
     */
    public static Calendar convertISO8610ToCalendar(String iso8601string) {
        Calendar calendar = GregorianCalendar.getInstance();
        String s = iso8601string.replace("Z", "+00:00");
        try {
            s = s.substring(0, 22) + s.substring(23); // to get rid of the ":"
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date date = formatter.parse(s);
            calendar.setTime(date);
        } catch (IndexOutOfBoundsException e) {
            //e.printStackTrace();
        } catch (ParseException e) {
            //e.printStackTrace();
        } finally {
            return calendar;
        }
    }
}

Related

  1. convertDateStringToString(String dateString, String format)
  2. convertDateStringToString(String strDate, String currentFormat, String parseFormat)
  3. convertDatetimeFormat(Date tar_date, String date_format)
  4. convertIso8601FormatToArsTime(String timeString)
  5. convertIso8601FormatToArsTimeOfDay(String timeOfDayString)
  6. convertTimeStringToTimestamp(String timeString, String timeFormat)