All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.up.uca.serialization.DateSaber Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.serialization;

import io.vertx.up.fn.Fn;
import io.vertx.up.util.Ut;

import java.util.Calendar;
import java.util.Date;

/**
 * Date, Calendar
 */
public class DateSaber extends BaseSaber {

    @Override
    public Object from(final Class paramType,
                       final String literal) {
        return Fn.getNull(() ->
                        Fn.getSemi(Date.class == paramType ||
                                        Calendar.class == paramType, getLogger(),
                                () -> {
                                    verifyInput(!Ut.isDate(literal), paramType, literal);
                                    final Date reference = Ut.parse(literal);
                                    if (Calendar.class == paramType) {
                                        // Specific date format
                                        final Calendar calendar = Calendar.getInstance();
                                        calendar.setTime(reference);
                                        return calendar;
                                    }
                                    return reference;
                                }, Date::new),
                paramType, literal);
    }

    @Override
    public  Object from(final T input) {
        return Fn.getNull(() -> {
            Object reference = null;
            if (input instanceof Date) {
                final Date date = (Date) input;
                reference = date.getTime();
            } else if (input instanceof Calendar) {
                final Calendar date = (Calendar) input;
                reference = date.getTime().getTime();
            }
            return reference;
        }, input);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy