forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unmarshalling dates using JAXB - examples (eugenp#7672)
- Loading branch information
1 parent
da781bf
commit 2a16590
Showing
9 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() + "]"; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookDateAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() + "]"; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/BookLocalDateTimeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() + "]"; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/DateAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Date> { | ||
|
||
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); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshalling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jaxb/src/main/java/com/baeldung/jaxb/dateunmarshalling/LocalDateTimeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, LocalDateTime> { | ||
|
||
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<book> | ||
<title>Book1</title> | ||
<published>1979-11-28 02:31:32</published> | ||
</book> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<book> | ||
<title>Book1</title> | ||
<published>1979-11-28T02:31:32</published> | ||
</book> |
52 changes: 52 additions & 0 deletions
52
jaxb/src/test/java/com/baeldung/jaxb/dateunmarshalling/JaxbDateUnmarshallingUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |