1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.date.iterator;
12
13 import java.text.DecimalFormat;
14 import java.text.NumberFormat;
15
16 import de.jaret.util.date.JaretDate;
17
18 /***
19 * Implementation of the DateIterator for hours.
20 *
21 * @author kliem
22 * @version $Id: HourIterator.java 828 2009-02-08 13:58:21Z kliem $
23 */
24 public class HourIterator extends AbstractDateIterator implements DateIterator {
25
26 /*** default formatter. */
27 protected IIteratorFormatter _defaultFormatter = new IIteratorFormatter() {
28
29 /***
30 * {@inheritDoc}
31 */
32 public String getLabel(JaretDate date, Format format) {
33 if (format.equals(Format.SHORT)) {
34 return date.getHours() + "h";
35 } else if (format.equals(Format.MEDIUM)) {
36 return NF.format(date.getHours()) + ":" + NF.format(date.getMinutes());
37 } else {
38 return NF.format(date.getHours()) + ":" + NF.format(date.getMinutes());
39 }
40 }
41 };
42
43 /*** default step if none is set. */
44 private static final int DEFAULT_STEP = 12;
45
46 /*** step for hours. */
47 protected int _hourStep = DEFAULT_STEP;
48
49 /*** Number format for labels. */
50 protected static final NumberFormat NF = new DecimalFormat();
51 static {
52 NF.setMaximumFractionDigits(0);
53 NF.setMinimumIntegerDigits(2);
54 }
55
56 /***
57 * Contructor supplying the setp.
58 *
59 * @param hourStep number of hours between steps
60 */
61 public HourIterator(int hourStep) {
62 _hourStep = hourStep;
63 }
64
65 /***
66 * {@inheritDoc}
67 */
68 protected void advanceDate(JaretDate date) {
69 date.advanceHours(_hourStep);
70 }
71
72 /***
73 * {@inheritDoc}
74 */
75 public long getApproxStepMilliSeconds() {
76 return _hourStep * 60 * 60 * 1000;
77 }
78
79 /***
80 * {@inheritDoc}
81 */
82 protected JaretDate correctStartDate(JaretDate date) {
83 date.setMinutes(0);
84 date.setSeconds(0);
85 date.setMilliseconds(0);
86
87 int diff = date.getHours() % _hourStep;
88 date.backHours(diff);
89
90 return date;
91 }
92
93 /***
94 * {@inheritDoc}
95 */
96 protected IIteratorFormatter getDefaultFormatter() {
97 return _defaultFormatter;
98 }
99
100 }