create XMLGregorianCalendar From LocalDateTime - Java java.time

Java examples for java.time:LocalDateTime

Description

create XMLGregorianCalendar From LocalDateTime

Demo Code


//package com.java2s;
import java.time.LocalDateTime;
import java.time.ZoneId;

import java.time.format.DateTimeFormatter;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    private static final DateTimeFormatter formatter = DateTimeFormatter
            .ofPattern("yyyy-MMM-dd kkmm").withZone(ZoneId.of("Z"));

    public static XMLGregorianCalendar createXMLGregorianFromLocalDateTime(
            String date, String time) throws DatatypeConfigurationException {
        LocalDateTime dateTime = parseDateAndTime(date, time);
        XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(dateTime.getYear(),
                        dateTime.getMonthValue(), dateTime.getDayOfMonth(),
                        dateTime.getHour(), dateTime.getMinute(),
                        dateTime.getSecond(), dateTime.getNano(), 0);
        return xmlCal;
    }/*from  w  w w  . ja v  a  2s .  c  o m*/

    public static LocalDateTime parseDateAndTime(String date, String time) {
        StringBuilder combined = new StringBuilder();
        combined.append(date).append(" ").append(time);

        return LocalDateTime.parse(combined.toString(), formatter);
    }
}

Related Tutorials