Java tutorial
//package com.java2s; /** * This file is part of the au-xml-util package * * Copyright Trenton D. Adams <trenton daught d daught adams at gmail daught ca> * * au-xml-util is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * au-xml-util 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with au-xml-util. If not, see <http://www.gnu.org/licenses/>. * * See the COPYING file for more information. */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { /** * Converts an XML xs:datetime string to a Calendar object. We do not mess * around with formatting issues. It either works, or it doesn't. * * @param xsDateTime the xs:datetime formatted string * * @return the Calendar object * * @throws ParseException if a parsing error occurs */ public static Calendar xsDateTimeToCalendar(final String xsDateTime) throws ParseException { Calendar newCal = null; if (xsDateTime != null) { // -06:00 goes to -0600 final String fixedTime = xsDateTime.replaceAll("([-+]??\\d{2}):??(\\d{2})$", "$1$2"); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); final Date date = dateFormat.parse(fixedTime); newCal = GregorianCalendar.getInstance(); newCal.setTime(date); } return newCal; } }