get Xml Time - Java XML

Java examples for XML:XML Calendar

Description

get Xml Time

Demo Code

/*//from w  ww  .j ava  2s . c  om
 * Copyright 2012 Ixonos Plc, Finland. All rights reserved.
 * Copyright 2012 Arcusys Oy, Finland. All rights reserved.
 *
 * This file is part of Kohti kumppanuutta.
 *
 * This file is licensed under GNU LGPL version 3.
 * Please see the 'license.txt' file in the root directory of the package you received.
 * If you did not receive a license, please contact the copyright holders (http://www.ixonos.com/, http://www.arcusys.fi/).
 * 
 */
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main{
    public static void main(String[] argv) throws Exception{
        Date date = new Date();
        int minutes = 2;
        System.out.println(getXmlTime(date,minutes));
    }
    public static XMLGregorianCalendar getXmlTime(final Date date,
            final int minutes) {
        return getXmlCalendar(date, new SetDateOnlyCalendar() {
            @Override
            void setCalendar(Calendar calendar) {
                super.setCalendar(calendar);
                calendar.set(Calendar.MINUTE, minutes);
            }
        });
    }
    private static XMLGregorianCalendar getXmlCalendar(final Date date,
            final SetCalendar setCalendar) {
        if (date == null) {
            return null;
        }
        try {
            final GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar
                    .getInstance();
            calendar.setTime(date);
            setCalendar.setCalendar(calendar);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(
                    calendar);
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials