Java XML Transform transformStringToXMLGregorianCalendar(String dateTime, String pattern)

Here you can find the source of transformStringToXMLGregorianCalendar(String dateTime, String pattern)

Description

transform String To XML Gregorian Calendar

License

Open Source License

Declaration

public static XMLGregorianCalendar transformStringToXMLGregorianCalendar(String dateTime, String pattern) 

Method Source Code


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

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

import javax.xml.datatype.DatatypeConfigurationException;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    public static XMLGregorianCalendar transformStringToXMLGregorianCalendar(String dateTime, String pattern) {
        XMLGregorianCalendar xmlGregorianCalendar;
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {//from   ww  w  . j  a v a  2  s  .c o m
            xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            Calendar calendar = Calendar.getInstance();
            calendar.setLenient(true);
            calendar.setTime(format.parse(dateTime));

            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setHour(calendar.get(Calendar.HOUR_OF_DAY));
            xmlGregorianCalendar.setMinute(calendar.get(Calendar.MINUTE));
            xmlGregorianCalendar.setSecond(calendar.get(Calendar.SECOND));
            // xmlGregorianCalendar.setMillisecond(calendar.get(Calendar.MILLISECOND));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;

    }
}

Related

  1. transformerFactory()
  2. transformFile(Source stylesheetFile, File input, File output)
  3. transformLogViaXSLT(String logFilePath, String xslFilePath)
  4. transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  5. transformStringToDoc(String input)
  6. transformToHTML(StreamSource xmlStreamSource)
  7. transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  8. transformToString(Source xmlSource, Source xslSource)
  9. transformViaXslt(String xml, String xslFile)