diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/Book.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/Book.java new file mode 100644 index 0000000000..0625c58344 --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/Book.java @@ -0,0 +1,25 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.datatype.XMLGregorianCalendar; + +@XmlRootElement(name = "book") +public class Book { + + @XmlElement(name = "title", required = true) + private String title; + + @XmlElement(name = "published", required = true) + private XMLGregorianCalendar published; + + public XMLGregorianCalendar getPublished() { + return published; + } + + @Override + public String toString() { + return "[title: " + title + "; published: " + published.toString() + "]"; + } + +} \ No newline at end of file diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookDateAdapter.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookDateAdapter.java new file mode 100644 index 0000000000..c882f37a04 --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookDateAdapter.java @@ -0,0 +1,27 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.Date; + +@XmlRootElement(name = "book") +public class BookDateAdapter { + + @XmlElement(name = "title", required = true) + private String title; + + @XmlElement(name = "published", required = true) + @XmlJavaTypeAdapter(DateAdapter.class) + private Date published; + + public Date getPublished() { + return published; + } + + @Override + public String toString() { + return "[title: " + title + "; published: " + published.toString() + "]"; + } + +} \ No newline at end of file diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookLocalDateTimeAdapter.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookLocalDateTimeAdapter.java new file mode 100644 index 0000000000..53a780a87a --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookLocalDateTimeAdapter.java @@ -0,0 +1,27 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.time.LocalDateTime; + +@XmlRootElement(name = "book") +public class BookLocalDateTimeAdapter { + + @XmlElement(name = "title", required = true) + private String title; + + @XmlElement(name = "published", required = true) + @XmlJavaTypeAdapter(LocalDateTimeAdapter.class) + private LocalDateTime published; + + public LocalDateTime getPublished() { + return published; + } + + @Override + public String toString() { + return "[title: " + title + "; published: " + published.toString() + "]"; + } + +} \ No newline at end of file diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java new file mode 100644 index 0000000000..3b0fd0bd26 --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java @@ -0,0 +1,21 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.annotation.adapters.XmlAdapter; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateAdapter extends XmlAdapter { + + private static final String CUSTOM_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss"; + + @Override + public String marshal(Date v) { + return new SimpleDateFormat(CUSTOM_FORMAT_STRING).format(v); + } + + @Override + public Date unmarshal(String v) throws Exception { + return new SimpleDateFormat(CUSTOM_FORMAT_STRING).parse(v); + } + +} \ No newline at end of file diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshalling.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshalling.java new file mode 100644 index 0000000000..205859b2bf --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshalling.java @@ -0,0 +1,45 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.InputStream; + +public class JaxbDateUnmarshalling { + + public static final String DEFAULT_DATE_UNMARSHALLING_FILE = "default-date-unmarshalling.xml"; + public static final String CUSTOM_DATE_UNMARSHALLING_FILE = "custom-date-unmarshalling.xml"; + + public static Book unmarshalDates(InputStream inputFile) throws JAXBException { + JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (Book) jaxbUnmarshaller.unmarshal(inputFile); + } + + public static BookDateAdapter unmarshalDatesUsingCustomXmlAdapter(InputStream inputFile) throws JAXBException { + JAXBContext jaxbContext = JAXBContext.newInstance(BookDateAdapter.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (BookDateAdapter) jaxbUnmarshaller.unmarshal(inputFile); + } + + public static BookLocalDateTimeAdapter unmarshalDatesUsingJava8(InputStream inputFile) throws JAXBException { + JAXBContext jaxbContext = JAXBContext.newInstance(BookLocalDateTimeAdapter.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (BookLocalDateTimeAdapter) jaxbUnmarshaller.unmarshal(inputFile); + } + + public static InputStream getInputStream(String file) { + ClassLoader classLoader = JaxbDateUnmarshalling.class.getClassLoader(); + return classLoader.getResourceAsStream(file); + } + + public static void main(String[] args) throws JAXBException { + Book book = unmarshalDates(getInputStream(DEFAULT_DATE_UNMARSHALLING_FILE)); + BookDateAdapter bookDateAdapter = unmarshalDatesUsingCustomXmlAdapter(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE)); + BookLocalDateTimeAdapter bookLocalDateTimeAdapter = unmarshalDatesUsingJava8(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE)); + System.out.println(book); + System.out.println(bookDateAdapter); + System.out.println(bookLocalDateTimeAdapter); + } + +} diff --git a/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/LocalDateTimeAdapter.java b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/LocalDateTimeAdapter.java new file mode 100644 index 0000000000..7fa224334c --- /dev/null +++ b/jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/LocalDateTimeAdapter.java @@ -0,0 +1,21 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import javax.xml.bind.annotation.adapters.XmlAdapter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class LocalDateTimeAdapter extends XmlAdapter { + + private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public String marshal(LocalDateTime dateTime) { + return dateTime.format(dateFormat); + } + + @Override + public LocalDateTime unmarshal(String dateTime) { + return LocalDateTime.parse(dateTime, dateFormat); + } + +} \ No newline at end of file diff --git a/jaxb/src/main/resources/custom-date-unmarshalling.xml b/jaxb/src/main/resources/custom-date-unmarshalling.xml new file mode 100644 index 0000000000..f4cc7a4f79 --- /dev/null +++ b/jaxb/src/main/resources/custom-date-unmarshalling.xml @@ -0,0 +1,5 @@ + + + Book1 + 1979-11-28 02:31:32 + \ No newline at end of file diff --git a/jaxb/src/main/resources/default-date-unmarshalling.xml b/jaxb/src/main/resources/default-date-unmarshalling.xml new file mode 100644 index 0000000000..44cfa7e6c5 --- /dev/null +++ b/jaxb/src/main/resources/default-date-unmarshalling.xml @@ -0,0 +1,5 @@ + + + Book1 + 1979-11-28T02:31:32 + \ No newline at end of file diff --git a/jaxb/src/test/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshallingUnitTest.java b/jaxb/src/test/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshallingUnitTest.java new file mode 100644 index 0000000000..298034be3d --- /dev/null +++ b/jaxb/src/test/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshallingUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.jaxb.dateunmarshalling; + +import org.junit.Test; + +import javax.xml.bind.JAXBException; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; + +import static org.junit.Assert.assertEquals; + +public class JaxbDateUnmarshallingUnitTest { + + @Test + public void whenUnmarshalDatesIsCalled_ThenCorrectDateIsReturned() throws JAXBException, DatatypeConfigurationException { + InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.DEFAULT_DATE_UNMARSHALLING_FILE); + XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar("1979-11-28T02:31:32"); + + Book book = JaxbDateUnmarshalling.unmarshalDates(inputStream); + + assertEquals(expected, book.getPublished()); + } + + @Test + public void whenUnmarshalDatesUsingCustomXmlAdapterIsCalled_ThenCorrectDateIsReturned() throws JAXBException, ParseException { + InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + Date expected = format.parse("1979-11-28 02:31:32"); + + BookDateAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingCustomXmlAdapter(inputStream); + + assertEquals(expected, book.getPublished()); + } + + @Test + public void whenUnmarshalDatesUsingJava8IsCalled_ThenCorrectDateIsReturned() throws JAXBException { + InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime expected = LocalDateTime.parse("1979-11-28 02:31:32", dateFormat); + + BookLocalDateTimeAdapter book = JaxbDateUnmarshalling.unmarshalDatesUsingJava8(inputStream); + + assertEquals(expected, book.getPublished()); + } + +} \ No newline at end of file