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

org.jnosql.artemis.cassandra.converters.TimestampConverter Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */
package org.jnosql.artemis.cassandra.converters;


import org.jnosql.artemis.AttributeConverter;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * The converter when the Cassandra type is timestamp.
 * This attribute converter has support to:
 * 

{@link Number}

*

{@link java.time.LocalDate}

*

{@link java.time.LocalDateTime}

*

{@link java.time.ZonedDateTime}

*

{@link Date}

*

{@link Calendar}

*/ public class TimestampConverter implements AttributeConverter { private static final ZoneId ZONE_ID = ZoneId.systemDefault(); @Override public Date convertToDatabaseColumn(Object attribute) { if (attribute == null) { return null; } if (Number.class.isInstance(attribute)) { return new Date(Number.class.cast(attribute).longValue()); } if (java.time.LocalDate.class.isInstance(attribute)) { return Date.from(java.time.LocalDate.class.cast(attribute).atStartOfDay(ZONE_ID).toInstant()); } if (LocalDateTime.class.isInstance(attribute)) { return Date.from(LocalDateTime.class.cast(attribute).atZone(ZONE_ID).toInstant()); } if (ZonedDateTime.class.isInstance(attribute)) { return Date.from(ZonedDateTime.class.cast(attribute).toInstant()); } if (Date.class.isInstance(attribute)) { return Date.class.cast(attribute); } if (Calendar.class.isInstance(attribute)) { return Calendar.class.cast(attribute).getTime(); } throw new IllegalArgumentException("There is not support to: " + attribute.getClass()); } @Override public Object convertToEntityAttribute(Date dbData) { return dbData.getTime(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy