-
-
Save newkek/46501e469efe2d983f639ba8f9f4e80a to your computer and use it in GitHub Desktop.
Custom Codec for Date type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CodecRegistry codecRegistry = new CodecRegistry(); | |
codecRegistry.register(new LocalDateCustomCodec()); | |
Cluster cluster = Cluster.builder() | |
.withCodecRegistry(codecRegistry) | |
.build(); | |
Session session = cluster.connect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.datastax.driver.core.DataType; | |
import com.datastax.driver.core.LocalDate; | |
import com.datastax.driver.core.ProtocolVersion; | |
import com.datastax.driver.core.TypeCodec; | |
import com.datastax.driver.core.exceptions.InvalidTypeException; | |
import java.nio.ByteBuffer; | |
public class LocalDateCustomCodec extends TypeCodec<LocalDate> { | |
public LocalDateCustomCodec() { | |
super(DataType.custom("org.apache.cassandra.db.marshal.SimpleDateType"), LocalDate.class); | |
} | |
@Override | |
public ByteBuffer serialize(LocalDate value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
return date().serialize(value, protocolVersion); | |
} | |
@Override | |
public LocalDate deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
return date().deserialize(bytes, protocolVersion); | |
} | |
@Override | |
public LocalDate parse(String value) throws InvalidTypeException { | |
return date().parse(value); | |
} | |
@Override | |
public String format(LocalDate value) throws InvalidTypeException { | |
return date().format(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment