Created
September 12, 2014 15:24
-
-
Save Sythelux/8171ab79056f5cc6dc8d to your computer and use it in GitHub Desktop.
Timestamp
This file contains hidden or 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 java.text.DateFormat; | |
| import java.util.Date; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| * <p>This Class provides Some Funktionality around Working with Timestamps, it provides the Funktionality to convert Timestamps into human readable Date Formats and it provides a Funktionality for accuracy.</p> | |
| * <p>Human readable Date Formats mean, that after you made a timestamp with Constructor or by setting them you can get a "DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)" formatted Date back. (that means something like "27.08.2014 13:37" in Germany. | |
| * you can also change it by using: setDateFormat() </p> | |
| * <p>Accuracy is something special, this means, that the internal Value is saved in the currency Accuracy gives</p> | |
| * <p> for Example: <br /> | |
| * new Timestamp(1407925608150).getFormatted() will print: 13.08.14 12:26<br /> | |
| * new Timestamp(1407925608150, TimeUnit.DAYS).getFormatted() will print: 13.08.14 02:00<br /> | |
| * new Timestamp(1407925608150, TimeUnit.HOURS).getFormatted() will print: 13.08.14 12:00<br /> | |
| * new Timestamp(1407925608150, TimeUnit.SECONDS).getFormatted() will print: 13.08.14 12:26<br /> | |
| * <br /> | |
| * the Second and the Third are obvious<br /> | |
| * the fourth looks the Same, but thats because of the Format if you call longValue() you see, that they all look different.<br /> | |
| * second: 1407888000000<br /> | |
| * third: 1407924000000<br /> | |
| * fourth: 1407925608000<br /> | |
| * At the Moment this also means, that there is Dataloss this will maybe be removed in future. | |
| * </p> | |
| * @author aschaub | |
| */ | |
| public class TimeStamp extends Number implements Comparable<Number> { | |
| private long timestamp; | |
| private TimeUnit accuracy = TimeUnit.MILLISECONDS; | |
| private DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); | |
| public TimeStamp() { | |
| } | |
| /** | |
| * | |
| * @param timestamp in milliseconds | |
| */ | |
| public TimeStamp(long timestamp) { | |
| this.timestamp = timestamp; | |
| } | |
| /** | |
| * | |
| * @param timestamp in milliseconds | |
| * @param accuracy | |
| */ | |
| public TimeStamp(long timestamp, TimeUnit accuracy) { | |
| this.timestamp = timestamp; | |
| setAccuracy(accuracy); | |
| } | |
| public void setTimestamp(long timestamp) { | |
| this.timestamp = TimeUnit.MILLISECONDS.convert(timestamp, accuracy); | |
| } | |
| public long getTimestamp() { | |
| return accuracy.toMillis(timestamp); | |
| } | |
| public Date getTimestampAsDate() { | |
| return new Date(getTimestamp()); | |
| } | |
| public String getFormatted() { | |
| return dateFormat.format(getTimestampAsDate()); | |
| } | |
| public void setAccuracy(TimeUnit newAccuracy) { | |
| timestamp = newAccuracy.convert(timestamp, accuracy); | |
| accuracy = newAccuracy; | |
| } | |
| public TimeUnit getAccuracy() { | |
| return accuracy; | |
| } | |
| @Override | |
| public int compareTo(Number o) { | |
| long retVal = 0; | |
| if (o instanceof TimeStamp) { | |
| TimeStamp ts = (TimeStamp) o; | |
| if (accuracy.compareTo(ts.accuracy) < 0) { | |
| retVal = ts.getAccuracy().convert(timestamp, accuracy) - ts.timestamp; | |
| retVal = ts.getAccuracy().toMillis(retVal); | |
| } else { | |
| retVal = timestamp - accuracy.convert(ts.timestamp, ts.getAccuracy()); | |
| retVal = accuracy.toMillis(retVal); | |
| } | |
| } else { | |
| retVal = longValue() - o.longValue(); | |
| } | |
| return (int) retVal; | |
| } | |
| @Override | |
| public int intValue() { | |
| return (int) getTimestamp(); | |
| } | |
| @Override | |
| public long longValue() { | |
| return getTimestamp(); | |
| } | |
| @Override | |
| public float floatValue() { | |
| return (float) getTimestamp(); | |
| } | |
| @Override | |
| public double doubleValue() { | |
| return (double) getTimestamp(); | |
| } | |
| public DateFormat getDateFormat() { | |
| return dateFormat; | |
| } | |
| public void setDateFormat(DateFormat dateFormat) { | |
| this.dateFormat = dateFormat; | |
| } | |
| @Override | |
| public String toString() { | |
| return longValue()+""; | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| TimeStamp ts1 = new TimeStamp(System.currentTimeMillis()); | |
| Thread.sleep(100); | |
| TimeStamp ts2 = new TimeStamp(System.currentTimeMillis(), TimeUnit.SECONDS); | |
| Thread.sleep(100); | |
| long t3 = System.currentTimeMillis(); | |
| System.out.println(ts1.compareTo(ts2)); | |
| System.out.println(ts2.compareTo(ts1)); | |
| System.out.println(ts1+"|"+ t3+"|"+ts1.compareTo(t3)); | |
| System.out.println(ts1.getFormatted()); | |
| System.out.println(ts2.getFormatted()); | |
| System.out.println(new TimeStamp(1407925608150L).getFormatted()); | |
| System.out.println(new TimeStamp(1407925608150L, TimeUnit.DAYS).getFormatted()+"|"+new TimeStamp(1407925608150L, TimeUnit.DAYS).longValue()); | |
| System.out.println(new TimeStamp(1407925608150L, TimeUnit.HOURS).getFormatted()+"|"+new TimeStamp(1407925608150L, TimeUnit.HOURS).longValue()); | |
| System.out.println(new TimeStamp(1407925608150L, TimeUnit.SECONDS).getFormatted()+"|"+new TimeStamp(1407925608150L, TimeUnit.SECONDS).longValue()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment