aboutsummaryrefslogtreecommitdiff
path: root/juick-core/src/main/java/com/juick/xml/adapters/SimpleDateAdapter.java
blob: 1093e47a111db573e1a1569cda978d8d84c6d2f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.juick.xml.adapters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Created by vitalyster on 15.11.2016.
 */

public class SimpleDateAdapter extends XmlAdapter<String, Date> {

    private final SimpleDateFormat dateFormat;
    public SimpleDateAdapter() {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    @Override
    public String marshal(Date v) throws Exception {
        if (v == null) {
            return null;
        }
        synchronized (dateFormat) {
            return dateFormat.format(v);
        }
    }

    @Override
    public Date unmarshal(String v) throws Exception {
        synchronized (dateFormat) {
            return dateFormat.parse(v);
        }
    }

}