PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/query/DateHelper.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 96 lines | 62 code | 16 blank | 18 comment | 0 complexity | 61c8d203caffccb6a857d87265aa7961 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.query;
18
19import java.sql.Timestamp;
20import java.text.DateFormat;
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
23import java.util.Date;
24
25/**
26 * @mdogan 4/26/12
27 */
28final class DateHelper {
29
30    static final String timestampFormat = "yyyy-MM-dd hh:mm:ss.SSS";
31    static final String dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
32    static final String sqlDateFormat = "yyyy-mm-dd";
33
34    static Date parseDate(final String value) {
35        try {
36            return getUtilDateFormat().parse(value);
37        } catch (ParseException e) {
38            return throwRuntimeParseException(value, e);
39        }
40    }
41
42    static Timestamp parseTimeStamp(final String value) {
43        try {
44            return new Timestamp(getTimestampFormat().parse(value).getTime());
45        } catch (ParseException e) {
46            return throwRuntimeParseException(value, e);
47        }
48    }
49
50    static java.sql.Date parseSqlDate(final String value) {
51        try {
52            return new java.sql.Date(getSqlDateFormat().parse(value).getTime());
53        } catch (ParseException e) {
54            return throwRuntimeParseException(value, e);
55        }
56    }
57
58    static Date tryParse(final String value) {
59        try {
60            return getUtilDateFormat().parse(value);
61        } catch (Exception ignored) {
62        }
63
64        try {
65            return getTimestampFormat().parse(value);
66        } catch (Exception ignored) {
67        }
68
69        try {
70            return getSqlDateFormat().parse(value);
71        } catch (Exception ignored) {
72        }
73
74        return throwRuntimeParseException(value, null);
75    }
76
77    private static <T> T throwRuntimeParseException(String value, Exception e) {
78        throw new RuntimeException("Unable to parse date from value: '" + value
79                + "' ! Valid formats are: '" + dateFormat + "', '" + timestampFormat
80                + "' and '" + sqlDateFormat + "'.", e);
81    }
82
83    private static DateFormat getTimestampFormat() {
84        return new SimpleDateFormat(timestampFormat);
85    }
86
87    private static DateFormat getSqlDateFormat() {
88        return new SimpleDateFormat(sqlDateFormat);
89    }
90
91    private static DateFormat getUtilDateFormat() {
92        return new SimpleDateFormat(dateFormat);
93    }
94
95    private DateHelper() {}
96}