/* Copyright (c) 2008, Centre for Educational Technology, Tallinn University Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package ee.tlu.htk.waramu.utils; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Formatter; /** * * @author vahur */ public class UTCTime extends Object implements Serializable, Comparable { static final long serialVersionUID = 1; private Date time; //construct the class with a Date object as input public UTCTime(Date time) { this.time = time; } //construct the class with a UTC string as input public UTCTime(String UTCtime) throws IllegalArgumentException { SimpleDateFormat sdf = null; try { if(UTCtime.contains("Z")) { sdf = new SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss'Z'"); } else { sdf = new SimpleDateFormat("yyyy-mm-dd"); } time = sdf.parse(UTCtime); } catch (Exception e) { e.printStackTrace(); throw new IllegalArgumentException("Invalid UTCdatetime string format"); } } //compare the UTCtimes public int compareTo(Object o) throws ClassCastException { UTCTime date = (UTCTime) o; return date.getDate().compareTo(time); } //return a Date object public Date getDate() { return time; } //return a UTC String @Override public String toString() { Formatter formatter = new Formatter(); try { formatter = formatter.format("%1$tY-%1$tm-%1$tdT%tTZ",time); } catch (Exception e) { e.printStackTrace(); } return formatter.toString(); } //Serialize the Date object out private void writeObject(ObjectOutputStream out) throws IOException { //out.writeLong(time.getTime()); out.writeObject(time); } //Serialize the Date object in private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { //time.setTime(in.readLong()); time = (Date) in.readObject(); } }