com.fincatto.documentofiscal.transformers.DFLocalDateTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nfe Show documentation
Show all versions of nfe Show documentation
Biblioteca de comunicacao de nota fiscal eletronica brasileira
package com.fincatto.documentofiscal.transformers;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.simpleframework.xml.transform.Transform;
public class DFLocalDateTransformer implements Transform {
private static final DateTimeFormatter DATATIME_FORMATTER_YYYYMMDDXXX = DateTimeFormatter.ofPattern("yyyy-MM-ddXXX");
private static final DateTimeFormatter DATATIME_FORMATTER_YYYYMMDDHHMMSSXXXX = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXXXX");
private static final DateTimeFormatter DATETIME_FORMATTER_YYYYMMDD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static final DateTimeFormatter DATETIME_FORMATTER_DDMMYYYY = DateTimeFormatter.ofPattern("dd/MM/yyyy");
private static final DateTimeFormatter DATETIME_FORMATTER_DDMMYYYHHMMSS = DateTimeFormatter.ofPattern("dd/MM/yyyy' 'HH:mm:ss");
@Override
public LocalDate read(final String data) {
try {
return LocalDate.parse(data, DFLocalDateTransformer.DATETIME_FORMATTER_YYYYMMDD);
} catch (final Exception e) {
try {
return LocalDate.from(DFLocalDateTransformer.DATATIME_FORMATTER_YYYYMMDDXXX.parse(data));
} catch (final Exception e2) {
try {
return LocalDate.from(DFLocalDateTransformer.DATETIME_FORMATTER_DDMMYYYY.parse(data));
} catch (final Exception e3) {
try {
return LocalDate.from(DFLocalDateTransformer.DATETIME_FORMATTER_DDMMYYYHHMMSS.parse(data));
} catch (final Exception e4) {
return LocalDate.from(DFLocalDateTransformer.DATATIME_FORMATTER_YYYYMMDDHHMMSSXXXX.parse(data));
}
}
}
}
}
@Override
public String write(final LocalDate data) {
return DFLocalDateTransformer.DATETIME_FORMATTER_YYYYMMDD.format(data);
}
}