]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/TimestampLocal.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / data / TimestampLocal.java
1 package tim.prune.data;
2
3 import java.text.DateFormat;
4 import java.util.Calendar;
5 import java.util.TimeZone;
6
7
8 /**
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.
13  */
14 public class TimestampLocal extends Timestamp
15 {
16         private boolean _valid = false;
17         private int _year=0, _month=0, _day=0;
18         private int _hour=0, _minute=0, _second=0;
19
20
21         /**
22          * Constructor giving each field value individually
23          * @param inYear year
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
29          */
30         public TimestampLocal(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond)
31         {
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;
38                 if (_valid)
39                 {
40                         _year = inYear;
41                         _month = inMonth;
42                         _day = inDay;
43                         _hour = inHour;
44                         _minute = inMinute;
45                         _second = inSecond;
46                 }
47         }
48
49
50         /** @return true if valid */
51         public boolean isValid()
52         {
53                 return _valid;
54         }
55
56         @Override
57         public Calendar getCalendar(TimeZone inZone)
58         {
59                 Calendar cal = Calendar.getInstance();
60                 if (inZone != null) {
61                         cal.setTimeZone(inZone);
62                 }
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);
70                 return cal;
71         }
72
73         @Override
74         public long getMilliseconds(TimeZone inZone)
75         {
76                 return getCalendar(inZone).getTimeInMillis();
77         }
78
79         @Override
80         public void addOffsetSeconds(long inOffset)
81         {
82                 System.err.println("Local timestamps don't support offsets.");
83         }
84
85         @Override
86         protected boolean hasMilliseconds()
87         {
88                 return false;
89         }
90
91         /**
92          * Utility method for formatting dates / times
93          * @param inFormat formatter object
94          * @param inTimezone timezone to use
95          * @return formatted String
96          */
97         @Override
98         protected String format(DateFormat inFormat, TimeZone inTimezone)
99         {
100                 Calendar cal = getCalendar(inTimezone);
101                 inFormat.setTimeZone(inTimezone);
102                 return inFormat.format(cal.getTime());
103         }
104 }