/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. package com.hazelcast.query;
  17. import java.sql.Timestamp;
  18. import java.text.DateFormat;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. /**
  23. * @mdogan 4/26/12
  24. */
  25. final class DateHelper {
  26. static final String timestampFormat = "yyyy-MM-dd hh:mm:ss.SSS";
  27. static final String dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
  28. static final String sqlDateFormat = "yyyy-mm-dd";
  29. static Date parseDate(final String value) {
  30. try {
  31. return getUtilDateFormat().parse(value);
  32. } catch (ParseException e) {
  33. return throwRuntimeParseException(value, e);
  34. }
  35. }
  36. static Timestamp parseTimeStamp(final String value) {
  37. try {
  38. return new Timestamp(getTimestampFormat().parse(value).getTime());
  39. } catch (ParseException e) {
  40. return throwRuntimeParseException(value, e);
  41. }
  42. }
  43. static java.sql.Date parseSqlDate(final String value) {
  44. try {
  45. return new java.sql.Date(getSqlDateFormat().parse(value).getTime());
  46. } catch (ParseException e) {
  47. return throwRuntimeParseException(value, e);
  48. }
  49. }
  50. static Date tryParse(final String value) {
  51. try {
  52. return getUtilDateFormat().parse(value);
  53. } catch (Exception ignored) {
  54. }
  55. try {
  56. return getTimestampFormat().parse(value);
  57. } catch (Exception ignored) {
  58. }
  59. try {
  60. return getSqlDateFormat().parse(value);
  61. } catch (Exception ignored) {
  62. }
  63. return throwRuntimeParseException(value, null);
  64. }
  65. private static <T> T throwRuntimeParseException(String value, Exception e) {
  66. throw new RuntimeException("Unable to parse date from value: '" + value
  67. + "' ! Valid formats are: '" + dateFormat + "', '" + timestampFormat
  68. + "' and '" + sqlDateFormat + "'.", e);
  69. }
  70. private static DateFormat getTimestampFormat() {
  71. return new SimpleDateFormat(timestampFormat);
  72. }
  73. private static DateFormat getSqlDateFormat() {
  74. return new SimpleDateFormat(sqlDateFormat);
  75. }
  76. private static DateFormat getUtilDateFormat() {
  77. return new SimpleDateFormat(dateFormat);
  78. }
  79. private DateHelper() {}
  80. }