您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SimpleDateParser.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.itmill.toolkit.terminal.gwt.client.util;
  2. import java.util.Date;
  3. /**
  4. * This is a simple regular expression based parser for date notations. While
  5. * our aim is to fully support in the future the JDK date parser, currently only
  6. * numeric notations and literals are supported such as
  7. * <code>dd/MM/yyyy HH:mm:ss.SSSS</code>. Each entity is parsed with the same
  8. * number of digits, i.e. for <code>dd</code> two digits will be parsed while
  9. * for <code>d</code> only one will be parsed.
  10. *
  11. * @author <a href="mailto:g.georgovassilis@gmail.com">George Georgovassilis</a>
  12. *
  13. */
  14. public class SimpleDateParser {
  15. private final static String DAY_IN_MONTH = "d";
  16. private final static String MONTH = "M";
  17. private final static String YEAR = "y";
  18. private final static String LITERAL = "\\";
  19. private final static int DATE_PATTERN = 0;
  20. private final static int REGEX_PATTERN = 1;
  21. private final static int COMPONENT = 2;
  22. private final static int REGEX = 0;
  23. private final static int INSTRUCTION = 1;
  24. private final static String[] TOKENS[] = {
  25. { "SSSS", "(\\d\\d\\d\\d)", DateLocale.TOKEN_MILLISECOND },
  26. { "SSS", "(\\d\\d\\d)", DateLocale.TOKEN_MILLISECOND },
  27. { "SS", "(\\d\\d)", DateLocale.TOKEN_MILLISECOND },
  28. { "S", "(\\d)", DateLocale.TOKEN_MILLISECOND },
  29. { "ss", "(\\d\\d)", DateLocale.TOKEN_SECOND },
  30. { "s", "(\\d\\d)", DateLocale.TOKEN_SECOND },
  31. { "mm", "(\\d\\d)", DateLocale.TOKEN_MINUTE },
  32. { "m", "(\\d\\d)", DateLocale.TOKEN_MINUTE },
  33. { "HH", "(\\d\\d)", DateLocale.TOKEN_HOUR_24 },
  34. { "H", "(\\d{1,2})", DateLocale.TOKEN_HOUR_24 },
  35. { "hh", "(\\d\\d)", DateLocale.TOKEN_HOUR_12 },
  36. { "h", "(\\d{1,2})", DateLocale.TOKEN_HOUR_12 },
  37. { "dd", "(\\d\\d)", DateLocale.TOKEN_DAY_OF_MONTH },
  38. { "d", "(\\d{1,2})", DateLocale.TOKEN_DAY_OF_MONTH },
  39. { "MM", "(\\d\\d)", DateLocale.TOKEN_MONTH },
  40. { "M", "(\\d{1,2})", DateLocale.TOKEN_MONTH },
  41. { "yyyy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR },
  42. { "yyy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR },
  43. { "yy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR },
  44. { "y", "(\\d{1,2})", DateLocale.TOKEN_YEAR },
  45. { "a", "(\\S{1,4})", DateLocale.TOKEN_AM_PM } };
  46. private Pattern regularExpression;
  47. private String instructions = "";
  48. private static void _parse(String format, String[] args) {
  49. if (format.length() == 0)
  50. return;
  51. if (format.startsWith("'")) {
  52. format = format.substring(1);
  53. int end = format.indexOf("'");
  54. if (end == -1)
  55. throw new IllegalArgumentException("Unmatched single quotes.");
  56. args[REGEX] += Pattern.quote(format.substring(0, end));
  57. format = format.substring(end + 1);
  58. }
  59. for (int i = 0; i < TOKENS.length; i++) {
  60. String[] row = TOKENS[i];
  61. String datePattern = row[DATE_PATTERN];
  62. if (!format.startsWith(datePattern))
  63. continue;
  64. format = format.substring(datePattern.length());
  65. args[REGEX] += row[REGEX_PATTERN];
  66. args[INSTRUCTION] += row[COMPONENT];
  67. _parse(format, args);
  68. return;
  69. }
  70. args[REGEX] += Pattern.quote("" + format.charAt(0));
  71. format = format.substring(1);
  72. _parse(format, args);
  73. }
  74. private static void load(Date date, String text, String component,
  75. String input, Pattern regex) {
  76. if (component.equals(DateLocale.TOKEN_MILLISECOND)) {
  77. date.setTime(date.getTime() / 1000 * 1000 + Integer.parseInt(text));
  78. }
  79. if (component.equals(DateLocale.TOKEN_SECOND)) {
  80. date.setSeconds(Integer.parseInt(text));
  81. }
  82. if (component.equals(DateLocale.TOKEN_MINUTE)) {
  83. date.setMinutes(Integer.parseInt(text));
  84. }
  85. if (component.equals(DateLocale.TOKEN_HOUR_24)) {
  86. date.setHours(Integer.parseInt(text));
  87. }
  88. if (component.equals(DateLocale.TOKEN_HOUR_12)) {
  89. int h = Integer.parseInt(text);
  90. String token = com.itmill.toolkit.terminal.gwt.client.DateLocale
  91. .getPM();
  92. String which = input.substring(input.length() - token.length()); // Assumes
  93. // both
  94. // AM
  95. // and
  96. // PM
  97. // tokens
  98. // have
  99. // same
  100. // length
  101. if (which.equalsIgnoreCase(token))
  102. h += 12;
  103. date.setHours(h);
  104. }
  105. if (component.equals(DateLocale.TOKEN_DAY_OF_MONTH)) {
  106. date.setDate(Integer.parseInt(text));
  107. }
  108. if (component.equals(DateLocale.TOKEN_MONTH)) {
  109. date.setMonth(Integer.parseInt(text) - 1);
  110. }
  111. if (component.equals(DateLocale.TOKEN_YEAR)) {
  112. // TODO: fix for short patterns
  113. date.setYear(Integer.parseInt(text) - 1900);
  114. }
  115. }
  116. public SimpleDateParser(String format) {
  117. String[] args = new String[] { "", "" };
  118. _parse(format, args);
  119. regularExpression = new Pattern(args[REGEX]);
  120. instructions = args[INSTRUCTION];
  121. }
  122. public Date parse(String input) {
  123. Date date = new Date(0, 0, 0, 0, 0, 0);
  124. String matches[] = regularExpression.match(input);
  125. if (matches == null)
  126. throw new IllegalArgumentException(input + " does not match "
  127. + regularExpression.pattern());
  128. if (matches.length - 1 != instructions.length())
  129. throw new IllegalArgumentException("Different group count - "
  130. + input + " does not match " + regularExpression.pattern());
  131. for (int group = 0; group < instructions.length(); group++) {
  132. String match = matches[group + 1];
  133. load(date, match, "" + instructions.charAt(group), input,
  134. regularExpression);
  135. }
  136. return date;
  137. }
  138. public static Date parse(String input, String pattern) {
  139. return new SimpleDateParser(pattern).parse(input);
  140. }
  141. }