0

There is jaxb adapter for date\time

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

public class DateAdapter extends XmlAdapter<String, Date> {
    private final SimpleDateFormat dateFormat = new 
                              SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS");

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

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

But how to make regex to make milli seconds - optional?

Cœur
  • 32,421
  • 21
  • 173
  • 232
Roberto
  • 784
  • 2
  • 12
  • 30
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jan 16 '19 at 15:29
  • Possible duplicate of [Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance](https://stackoverflow.com/questions/30090710/java-8-datetimeformatter-parsing-for-optional-fractional-seconds-of-varying-sign). It’s always a good idea to search before posting a question. – Ole V.V. Jan 16 '19 at 15:32

0 Answers0