Skip to content

Commit c97d364

Browse files
unmarshalling dates using JAXB - examples
1 parent 9d825c4 commit c97d364

File tree

9 files changed

+257
-0
lines changed

9 files changed

+257
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlElement;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
import javax.xml.datatype.XMLGregorianCalendar;
8+
9+
@XmlRootElement(name = "book")
10+
@XmlAccessorType(XmlAccessType.NONE)
11+
public class Book {
12+
13+
@XmlElement(name = "title", required = true)
14+
private String title;
15+
16+
@XmlElement(name = "published", required = true)
17+
private XMLGregorianCalendar published;
18+
19+
public Book() {
20+
}
21+
22+
public XMLGregorianCalendar getPublished() {
23+
return published;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "[title: " + title + "; published: " + published.toString() + "]";
29+
}
30+
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlElement;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
8+
import java.util.Date;
9+
10+
@XmlRootElement(name = "book")
11+
@XmlAccessorType(XmlAccessType.NONE)
12+
public class Book2 {
13+
14+
@XmlElement(name = "title", required = true)
15+
private String title;
16+
17+
@XmlElement(name = "published", required = true)
18+
@XmlJavaTypeAdapter(DateAdapter.class)
19+
private Date published;
20+
21+
public Book2() {
22+
}
23+
24+
public Date getPublished() {
25+
return published;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "[title: " + title + "; published: " + published.toString() + "]";
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlElement;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
8+
import java.time.LocalDateTime;
9+
10+
@XmlRootElement(name = "book")
11+
@XmlAccessorType(XmlAccessType.NONE)
12+
public class Book3 {
13+
14+
@XmlElement(name = "title", required = true)
15+
private String title;
16+
17+
@XmlElement(name = "published", required = true)
18+
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
19+
private LocalDateTime published;
20+
21+
public Book3() {
22+
}
23+
24+
public LocalDateTime getPublished() {
25+
return published;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "[title: " + title + "; published: " + published.toString() + "]";
31+
}
32+
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.annotation.adapters.XmlAdapter;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
public class DateAdapter extends XmlAdapter<String, Date> {
8+
9+
private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
10+
11+
@Override
12+
public String marshal(Date v) {
13+
synchronized (dateFormat) {
14+
return dateFormat.format(v);
15+
}
16+
}
17+
18+
@Override
19+
public Date unmarshal(String v) throws Exception {
20+
synchronized (dateFormat) {
21+
return dateFormat.parse(v);
22+
}
23+
}
24+
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.JAXBContext;
4+
import javax.xml.bind.JAXBException;
5+
import javax.xml.bind.Unmarshaller;
6+
import java.io.InputStream;
7+
8+
public class JaxbDateUnmarshalling {
9+
10+
public static final String DEFAULT_DATE_UNMARSHALLING_FILE = "default-date-unmarshalling.xml";
11+
public static final String CUSTOM_DATE_UNMARSHALLING_FILE = "custom-date-unmarshalling.xml";
12+
13+
public static Book unmarshalDates(InputStream inputFile) throws JAXBException {
14+
JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
15+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
16+
Book book = (Book) jaxbUnmarshaller.unmarshal(inputFile);
17+
return book;
18+
}
19+
20+
public static Book2 unmarshalDatesUsingCustomXmlAdapter(InputStream inputFile) throws JAXBException {
21+
JAXBContext jaxbContext = JAXBContext.newInstance(Book2.class);
22+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
23+
Book2 book = (Book2) jaxbUnmarshaller.unmarshal(inputFile);
24+
return book;
25+
}
26+
27+
public static Book3 unmarshalDatesUsingJava8(InputStream inputFile) throws JAXBException {
28+
JAXBContext jaxbContext = JAXBContext.newInstance(Book3.class);
29+
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
30+
Book3 book = (Book3) jaxbUnmarshaller.unmarshal(inputFile);
31+
return book;
32+
}
33+
34+
public static InputStream getInputStream(String file) {
35+
ClassLoader classLoader = JaxbDateUnmarshalling.class.getClassLoader();
36+
return classLoader.getResourceAsStream(file);
37+
}
38+
39+
public static void main(String[] args) {
40+
try {
41+
Book book = unmarshalDates(getInputStream(DEFAULT_DATE_UNMARSHALLING_FILE));
42+
Book2 book2 = unmarshalDatesUsingCustomXmlAdapter(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE));
43+
Book3 book3 = unmarshalDatesUsingJava8(getInputStream(CUSTOM_DATE_UNMARSHALLING_FILE));
44+
System.out.println(book);
45+
System.out.println(book2);
46+
System.out.println(book3);
47+
} catch (JAXBException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import javax.xml.bind.annotation.adapters.XmlAdapter;
4+
import java.time.LocalDateTime;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
8+
9+
private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
10+
11+
@Override
12+
public String marshal(LocalDateTime dateTime) {
13+
return dateTime.format(dateFormat);
14+
}
15+
16+
@Override
17+
public LocalDateTime unmarshal(String dateTime) {
18+
return LocalDateTime.parse(dateTime, dateFormat);
19+
}
20+
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<book>
3+
<title>Book1</title>
4+
<published>1979-11-28 02:31:32</published>
5+
</book>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<book>
3+
<title>Book1</title>
4+
<published>1979-11-28T02:31:32</published>
5+
</book>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.jaxb.dateunmarshalling;
2+
3+
import org.junit.Test;
4+
5+
import javax.xml.bind.JAXBException;
6+
import javax.xml.datatype.DatatypeConfigurationException;
7+
import javax.xml.datatype.DatatypeFactory;
8+
import javax.xml.datatype.XMLGregorianCalendar;
9+
import java.io.InputStream;
10+
import java.text.ParseException;
11+
import java.text.SimpleDateFormat;
12+
import java.time.LocalDateTime;
13+
import java.time.format.DateTimeFormatter;
14+
import java.util.Date;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
public class JaxbDateUnmarshallingUnitTest {
19+
20+
@Test
21+
public void whenUnmarshalDatesIsCalled_ThenCorrectDateIsReturned() throws JAXBException, DatatypeConfigurationException {
22+
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.DEFAULT_DATE_UNMARSHALLING_FILE);
23+
XMLGregorianCalendar expected = DatatypeFactory.newInstance().newXMLGregorianCalendar("1979-11-28T02:31:32");
24+
25+
Book book = JaxbDateUnmarshalling.unmarshalDates(inputStream);
26+
27+
assertEquals(expected, book.getPublished());
28+
}
29+
30+
@Test
31+
public void whenUnmarshalDatesUsingCustomXmlAdapterIsCalled_ThenCorrectDateIsReturned() throws JAXBException, ParseException {
32+
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
33+
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
34+
Date expected = format.parse("1979-11-28 02:31:32");
35+
36+
Book2 book = JaxbDateUnmarshalling.unmarshalDatesUsingCustomXmlAdapter(inputStream);
37+
38+
assertEquals(expected, book.getPublished());
39+
}
40+
41+
@Test
42+
public void whenUnmarshalDatesUsingJava8IsCalled_ThenCorrectDateIsReturned() throws JAXBException {
43+
InputStream inputStream = JaxbDateUnmarshalling.getInputStream(JaxbDateUnmarshalling.CUSTOM_DATE_UNMARSHALLING_FILE);
44+
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
45+
LocalDateTime expected = LocalDateTime.parse("1979-11-28 02:31:32", dateFormat);
46+
47+
Book3 book = JaxbDateUnmarshalling.unmarshalDatesUsingJava8(inputStream);
48+
49+
assertEquals(expected, book.getPublished());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)