Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.j256.ormlite.examples.datapersister;
 import java.util.Date;
A custom persister that tweaks how the Date is stored in the database. If the date is before 1970 then this just return null instead of throwing some sort of out-of-range error. This is useful because sometimes we get low values of SQL timestamps that are stored in existing schemas which can't be mapped to a Date.

You would not want this to override the default behavior for Date so you would use this by specifying the class in com.j256.ormlite.field.DatabaseField.persisterClass().

public class MyDatePersister extends DateType {
	private static final MyDatePersister singleTon = new MyDatePersister();
	private static final Timestamp ZERO_TIMESTAMP = new Timestamp(1970, 0, 0, 0, 0, 0, 0);
	private MyDatePersister() {
		super(.new Class<?>[] { Date.class });
	public static MyDatePersister getSingleton() {
		return ;
	public Object resultToSqlArg(FieldType fieldTypeDatabaseResults resultsint columnPosthrows SQLException {
		Timestamp timestamp = results.getTimestamp(columnPos);
		if (timestamp == null || .after(timestamp)) {
			return null;
else {
			return timestamp.getTime();
	public Object sqlArgToJava(FieldType fieldTypeObject sqlArgint columnPos) {
		if (sqlArg == null) {
			return null;
else {
			return new Date((LongsqlArg);
New to GrepCode? Check out our FAQ X