Skip to content

Instantly share code, notes, and snippets.

@newkek
Last active May 30, 2017 21:47
Show Gist options
  • Save newkek/46501e469efe2d983f639ba8f9f4e80a to your computer and use it in GitHub Desktop.
Save newkek/46501e469efe2d983f639ba8f9f4e80a to your computer and use it in GitHub Desktop.
Custom Codec for Date type
CodecRegistry codecRegistry = new CodecRegistry();
codecRegistry.register(new LocalDateCustomCodec());
Cluster cluster = Cluster.builder()
.withCodecRegistry(codecRegistry)
.build();
Session session = cluster.connect();
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