ir.iais.utilities.javautils.utils.xml.DateAdapter.java Source code

Java tutorial

Introduction

Here is the source code for ir.iais.utilities.javautils.utils.xml.DateAdapter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ir.iais.utilities.javautils.utils.xml;

import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;

/** @author yoones */
public class DateAdapter extends XmlAdapter<String, Date> {

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

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

    @Override
    public Date unmarshal(String v) throws Exception {
        return StringUtils.isEmpty(v) ? null : dateFormat.parse(v);
    }
}