Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Last active March 24, 2016 11:34
Show Gist options
  • Save Jeevuz/1691d9b79b323f848a9d to your computer and use it in GitHub Desktop.
Save Jeevuz/1691d9b79b323f848a9d to your computer and use it in GitHub Desktop.
java.sql.Timestamp subclass with ability to parse and hold the raw server string with microseconds and optional format parts. Has usefull method daysFromToday()
package com.example;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Extends the {@link java.sql.Timestamp} to parse server datetime.
* Additionally stores the raw datetime input.
* @author Vasili Chyrvon ([email protected])
*/
public class Timestamp extends java.sql.Timestamp {
// The regex pattern of 2016-03-11T05:10:12.000001-03:50 with groups. Microseconds and timezone is optional.
private static final Pattern SERVER_DATE_PATTERN = Pattern.compile("([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})\\.?([0-9]{6})?([+|-][0-9|\\:]{5})?");
private static final String DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZZZZZ";
private static final String DEFAULT_TIMEZONE = "+00:00";
public static final String NANO_NULLS_SUFFIX = "000";
private String mRaw; // Server date
public String getRaw() {
return mRaw;
}
void setRaw(String raw) {
mRaw = raw;
}
@SuppressWarnings("deprecation")
public static Timestamp create(@NonNull String raw) {
String dateTime;
String microseconds;
String timeZone;
// Parse
Matcher matcher = SERVER_DATE_PATTERN.matcher(raw.trim());
if (matcher.find()) {
dateTime = matcher.group(1);
microseconds = matcher.group(2);
timeZone = matcher.group(3);
} else {
throw new IllegalArgumentException("Server datetime (" + raw + ") not in the correct format!");
}
timeZone = (!TextUtils.isEmpty(timeZone)) ? timeZone : DEFAULT_TIMEZONE;
// Get date
String dateTimeAndTimeZone = dateTime + timeZone;
DateFormat dateFormat = new SimpleDateFormat(DATETIME_FORMAT, Locale.US);
Date date;
try {
date = dateFormat.parse(dateTimeAndTimeZone);
} catch (ParseException e) {
throw new RuntimeException("Can't parse '" + dateTimeAndTimeZone + "'!");
}
// Create and return timestamp
Timestamp timestamp = new Timestamp(date.getTime());
if (!TextUtils.isEmpty(microseconds)) {
timestamp.setNanos(Integer.parseInt(microseconds + NANO_NULLS_SUFFIX));
}
timestamp.setRaw(raw); // Save raw
return timestamp;
}
/**
* @deprecated Use {@link #create(String)} method instead
*/
@Deprecated
public Timestamp(long theTime) {
super(theTime);
}
public int daysFromToday() {
long theDayMillis = getTime();
theDayMillis = theDayMillis + TimeZone.getDefault().getOffset(theDayMillis);
long todayMillis = System.currentTimeMillis();
todayMillis = todayMillis + TimeZone.getDefault().getOffset(todayMillis);
long theDay = TimeUnit.MILLISECONDS.toDays(theDayMillis);
long today = TimeUnit.MILLISECONDS.toDays(todayMillis);
int difference = (int) (theDay - today);
// Timber.d("daysFromToday(): theDay = %s,today = %s, difference => %s", theDay, today, difference);
return difference;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment