Skip to content

Instantly share code, notes, and snippets.

@kshoji
Created May 11, 2015 09:00
Show Gist options
  • Save kshoji/babcbffb117634cdd2b0 to your computer and use it in GitHub Desktop.
Save kshoji/babcbffb117634cdd2b0 to your computer and use it in GitHub Desktop.
Add offset to Time, for WatchFace app on the Android Wear
package jp.kshoji.watchface;
import android.support.annotation.NonNull;
import android.text.format.Time;
/**
* <h2>Time with Offset</h2>
*
* For example, construct an instance with `new Offset(300);`,
* When the real time: `9:00`, this OffsetTime returns `9:05`
*
* @author K.Shoji
* @license <a href="http://www.apache.org/licenses/LICENSE-2.0.html">Apache License, Version 2.0</a>
*/
public class OffsetTime extends Time {
private long offsetMillis;
/**
* Default constructor; Same behaviour as {@link Time}
*/
public OffsetTime() {
offsetMillis = 0L;
}
/**
* Construct with offset
*
* @param offsetSeconds Time offsets in seconds
*/
public OffsetTime(int offsetSeconds) {
offsetMillis = offsetSeconds * 1000L;
}
@Override
public void set(long millis) {
super.set(millis + offsetMillis);
}
@Override
public void set(int monthDay, int month, int year) {
super.set(monthDay, month, year);
set(toMillis(false) + offsetMillis);
}
@Override
public void set(int second, int minute, int hour, int monthDay, int month, int year) {
super.set(second, minute, hour, monthDay, month, year);
set(toMillis(false) + offsetMillis);
}
@Override
public void set(@NonNull Time that) {
super.set(that);
set(toMillis(false) + offsetMillis);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment