1 package tim.prune.data;
3 import java.text.DateFormat;
4 import java.util.Calendar;
5 import java.util.TimeZone;
9 * Class to hold a timestamp based on a local timezone, for example
10 * from a camera or audio recorder.
11 * When the selected timezone changes, this timestamp will keep its
12 * date and time but the numerical value will change accordingly.
14 public class TimestampLocal extends Timestamp
16 private boolean _valid = false;
17 private int _year=0, _month=0, _day=0;
18 private int _hour=0, _minute=0, _second=0;
22 * Constructor giving each field value individually
24 * @param inMonth month, beginning with 1
25 * @param inDay day of month, beginning with 1
26 * @param inHour hour of day, 0-24
27 * @param inMinute minute
28 * @param inSecond seconds
30 public TimestampLocal(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
32 _valid = inYear > 0 && inYear < 3000
33 && inMonth > 0 && inMonth < 13
34 && inDay > 0 && inDay < 32
35 && inHour >= 0 && inHour < 24
36 && inMinute >= 0 && inMinute < 60
37 && inSecond >= 0 && inSecond < 60;
50 /** @return true if valid */
51 public boolean isValid()
57 public Calendar getCalendar(TimeZone inZone)
59 Calendar cal = Calendar.getInstance();
61 cal.setTimeZone(inZone);
63 cal.set(Calendar.YEAR, _year);
64 cal.set(Calendar.MONTH, _month - 1);
65 cal.set(Calendar.DAY_OF_MONTH, _day);
66 cal.set(Calendar.HOUR_OF_DAY, _hour);
67 cal.set(Calendar.MINUTE, _minute);
68 cal.set(Calendar.SECOND, _second);
69 cal.set(Calendar.MILLISECOND, 0);
74 public long getMilliseconds(TimeZone inZone)
76 return getCalendar(inZone).getTimeInMillis();
80 public void addOffsetSeconds(long inOffset)
82 System.err.println("Local timestamps don't support offsets.");
86 protected boolean hasMilliseconds()
92 * Utility method for formatting dates / times
93 * @param inFormat formatter object
94 * @param inTimezone timezone to use, or null
95 * @return formatted String
98 protected String format(DateFormat inFormat, TimeZone inTimezone)
100 Calendar cal = getCalendar(inTimezone);
101 if (inTimezone != null)
103 inFormat.setTimeZone(inTimezone);
105 return inFormat.format(cal.getTime());