summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
0 files changed, 0 insertions, 0 deletions
te-minor-deps'>update-minor-deps Vaadin 6, 7, 8 is a Java framework for modern Java web applications: https://github.com/vaadin/frameworkwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/DateTimeService.java
blob: e7b93475bc788d6d1bf66a7f237414f68d585847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal.gwt.client;

import java.util.Date;

import com.vaadin.terminal.gwt.client.ui.VDateField;

/**
 * This class provides date/time parsing services to all components on the
 * client side.
 * 
 * @author IT Mill Ltd.
 * 
 */
@SuppressWarnings("deprecation")
public class DateTimeService {

    private String currentLocale;

    private static int[] maxDaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
            31, 30, 31 };

    /**
     * Creates a new date time service with the application default locale.
     */
    public DateTimeService() {
        currentLocale = LocaleService.getDefaultLocale();
    }

    /**
     * Creates a new date time service with a given locale.
     * 
     * @param locale
     *            e.g. fi, en etc.
     * @throws LocaleNotLoadedException
     */
    public DateTimeService(String locale) throws LocaleNotLoadedException {
        setLocale(locale);
    }

    public void setLocale(String locale) throws LocaleNotLoadedException {
        if (LocaleService.getAvailableLocales().contains(locale)) {
            currentLocale = locale;
        } else {
            throw new LocaleNotLoadedException(locale);
        }
    }

    public String getLocale() {
        return currentLocale;
    }

    public String getMonth(int month) {
        try {
            return LocaleService.getMonthNames(currentLocale)[month];
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return null;
    }

    public String getShortMonth(int month) {
        try {
            return LocaleService.getShortMonthNames(currentLocale)[month];
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return null;
    }

    public String getDay(int day) {
        try {
            return LocaleService.getDayNames(currentLocale)[day];
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return null;
    }

    public String getShortDay(int day) {
        try {
            return LocaleService.getShortDayNames(currentLocale)[day];
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return null;
    }

    public int getFirstDayOfWeek() {
        try {
            return LocaleService.getFirstDayOfWeek(currentLocale);
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return 0;
    }

    public boolean isTwelveHourClock() {
        try {
            return LocaleService.isTwelveHourClock(currentLocale);
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return false;
    }

    public String getClockDelimeter() {
        try {
            return LocaleService.getClockDelimiter(currentLocale);
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        return ":";
    }

    public String[] getAmPmStrings() {
        try {
            return LocaleService.getAmPmStrings(currentLocale);
        } catch (final LocaleNotLoadedException e) {
            ClientExceptionHandler.displayError(e);
        }
        final String[] temp = new String[2];
        temp[0] = "AM";
        temp[1] = "PM";
        return temp;
    }

    public int getStartWeekDay(Date date) {
        final Date dateForFirstOfThisMonth = new Date(date.getYear(), date
                .getMonth(), 1);
        int firstDay;
        try {
            firstDay = LocaleService.getFirstDayOfWeek(currentLocale);
        } catch (final LocaleNotLoadedException e) {
            firstDay = 0;
            ClientExceptionHandler.displayError(e);
        }
        int start = dateForFirstOfThisMonth.getDay() - firstDay;
        if (start < 0) {
            start = 6;
        }
        return start;
    }

    public static void setMilliseconds(Date date, int ms) {
        date.setTime(date.getTime() / 1000 * 1000 + ms);
    }

    public static int getMilliseconds(Date date) {
        if (date == null) {
            return 0;
        }
    
        return (int) (date.getTime() - date.getTime() / 1000 * 1000);
    }

    public static int getNumberOfDaysInMonth(Date date) {
        final int month = date.getMonth();
        if (month == 1 && true == isLeapYear(date)) {
            return 29;
        }
        return maxDaysInMonth[month];
    }

    public static boolean isLeapYear(Date date) {
        // Instantiate the date for 1st March of that year
        final Date firstMarch = new Date(date.getYear(), 2, 1);

        // Go back 1 day
        final long firstMarchTime = firstMarch.getTime();
        final long lastDayTimeFeb = firstMarchTime - (24 * 60 * 60 * 1000); // NUM_MILLISECS_A_DAY

        // Instantiate new Date with this time
        final Date febLastDay = new Date(lastDayTimeFeb);

        // Check for date in this new instance
        return (29 == febLastDay.getDate()) ? true : false;
    }

    public static boolean isSameDay(Date d1, Date d2) {
        return (getDayInt(d1) == getDayInt(d2));
    }

    public static boolean isInRange(Date date, Date rangeStart, Date rangeEnd,
            int resolution) {
        Date s;
        Date e;
        if (rangeStart.after(rangeEnd)) {
            s = rangeEnd;
            e = rangeStart;
        } else {
            e = rangeEnd;
            s = rangeStart;
        }
        long start = s.getYear() * 10000000000l;
        long end = e.getYear() * 10000000000l;
        long target = date.getYear() * 10000000000l;

        if (resolution == VDateField.RESOLUTION_YEAR) {
            return (start <= target && end >= target);
        }
        start += s.getMonth() * 100000000l;
        end += e.getMonth() * 100000000l;
        target += date.getMonth() * 100000000l;
        if (resolution == VDateField.RESOLUTION_MONTH) {
            return (start <= target && end >= target);
        }
        start += s.getDate() * 1000000l;
        end += e.getDate() * 1000000l;
        target += date.getDate() * 1000000l;
        if (resolution == VDateField.RESOLUTION_DAY) {
            return (start <= target && end >= target);
        }
        start += s.getHours() * 10000l;
        end += e.getHours() * 10000l;
        target += date.getHours() * 10000l;
        if (resolution == VDateField.RESOLUTION_HOUR) {
            return (start <= target && end >= target);
        }
        start += s.getMinutes() * 100l;
        end += e.getMinutes() * 100l;
        target += date.getMinutes() * 100l;
        if (resolution == VDateField.RESOLUTION_MIN) {
            return (start <= target && end >= target);
        }
        start += s.getSeconds();
        end += e.getSeconds();
        target += date.getSeconds();
        return (start <= target && end >= target);

    }

    private static int getDayInt(Date date) {
        final int y = date.getYear();
        final int m = date.getMonth();
        final int d = date.getDate();

        return ((y + 1900) * 10000 + m * 100 + d) * 1000000000;
    }

    /**
     * Returns the ISO-8601 week number of the given date.
     * 
     * @param date
     *            The date for which the week number should be resolved
     * @return The ISO-8601 week number for {@literal date}
     */
    public static int getISOWeekNumber(Date date) {
        final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
        int dayOfWeek = date.getDay(); // 0 == sunday

        // ISO 8601 use weeks that start on monday so we use
        // mon=1,tue=2,...sun=7;
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        // Find nearest thursday (defines the week in ISO 8601). The week number
        // for the nearest thursday is the same as for the target date.
        int nearestThursdayDiff = 4 - dayOfWeek; // 4 is thursday
        Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff
                * MILLISECONDS_PER_DAY);

        Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1);
        long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime();
        int daysSinceFirstOfJanuary = (int) (timeDiff / MILLISECONDS_PER_DAY);

        int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1;

        return weekNumber;
    }

}