You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleDateFormat.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
  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.itmill.toolkit.terminal.gwt.client.util;
  17. import java.util.Date;
  18. /**
  19. * <dl>
  20. * <dt><b>Title: </b>
  21. * <dd>SimpleDateFormat</dd>
  22. * <p>
  23. * <dt><b>Description: </b>
  24. * <dd>GWT does not implement any of the java.text package, so this class tries
  25. * to fill the void of the missing java.text.SimpleDateFormat class. This
  26. * version however only supports a subset of the date and time patterns
  27. * supported by its java.text counterpart. The pattern symbols supported by this
  28. * class are:
  29. * <dl>
  30. * <dt><b>E</b></dt>
  31. * <dd>Day in a week</dd>
  32. * <dt><b>d</b></dt>
  33. * <dd>Day of the month</dd>
  34. * <dt><b>y</b></dt>
  35. * <dd>Year</dd>
  36. * <dt><b>M</b></dt>
  37. * <dd>Month January, Jan, 01, 1</dd>
  38. * <dt><b>H</b></dt>
  39. * <dd>Hour in 24 hour format (0-23)</dd>
  40. * <dt><b>h</b></dt>
  41. * <dd>Hour in 12 hour format (1-12)</dd>
  42. * <dt><b>m</b></dt>
  43. * <dd>Minute of the hour </dd>
  44. * <dt><b>s</b></dt>
  45. * <dd>Seconds of the minute</dd>
  46. * <dt><b>a</b></dt>
  47. * <dd>am/pm</dd>
  48. * </dl>
  49. * All characters that are not recognised as a date format character are
  50. * translated literally into the output string. <br/> </dd>
  51. * <p>
  52. * </dl>
  53. * <p>
  54. * A simple date parsing facility has also been implemented resembling the java
  55. * prototype. You can currently parse most numeric patterns but no temporal
  56. * literals (such as day or month names).
  57. * </p>
  58. *
  59. * @author <a href="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
  60. * @author <a href="mailto:g.georgovassilis@gmail.com">George Georgovassilis</a>
  61. * @version $Revision: 0.0 $
  62. */
  63. public class SimpleDateFormat {
  64. private String format;
  65. private DateLocale locale = new DateLocale();
  66. /**
  67. * Gets the support locale for formatting and parsing dates
  68. *
  69. * @return
  70. */
  71. public DateLocale getLocale() {
  72. return locale;
  73. }
  74. public void setLocale(DateLocale locale) {
  75. this.locale = locale;
  76. }
  77. public SimpleDateFormat(String pattern) {
  78. format = pattern;
  79. }
  80. public String format(Date date) {
  81. String f = "";
  82. if (format != null && format.length() > 0) {
  83. String lastTokenType = null;
  84. String currentToken = "";
  85. for (int i = 0; i < format.length(); i++) {
  86. String thisChar = format.substring(i, i + 1);
  87. String currentTokenType = DateLocale.SUPPORTED_DF_TOKENS
  88. .contains(thisChar) ? thisChar : "";
  89. if (currentTokenType.equals(lastTokenType) || i == 0) {
  90. currentToken += thisChar;
  91. lastTokenType = currentTokenType;
  92. } else {
  93. if ("".equals(lastTokenType))
  94. f += currentToken;
  95. else
  96. f += handleToken(currentToken, date);
  97. currentToken = thisChar;
  98. lastTokenType = currentTokenType;
  99. }
  100. }
  101. if ("".equals(lastTokenType))
  102. f += currentToken;
  103. else
  104. f += handleToken(currentToken, date);
  105. }
  106. return f;
  107. }
  108. /**
  109. * takes a date format string and returns the formatted portion of the date.
  110. * For instance if the token is MMMM then the full month name is returned.
  111. *
  112. * @param token
  113. * date format token
  114. * @param date
  115. * date to format
  116. * @return formatted portion of the date
  117. */
  118. private String handleToken(String token, Date date) {
  119. String response = token;
  120. String tc = token.substring(0, 1);
  121. if (DateLocale.TOKEN_DAY_OF_WEEK.equals(tc)) {
  122. if (token.length() > 3)
  123. response = locale.getWEEKDAY_LONG()[date.getDay()];
  124. else
  125. response = locale.getWEEKDAY_SHORT()[date.getDay()];
  126. } else if (DateLocale.TOKEN_DAY_OF_MONTH.equals(tc)) {
  127. if (token.length() == 1)
  128. response = Integer.toString(date.getDate());
  129. else
  130. response = twoCharDateField(date.getDate());
  131. } else if (DateLocale.TOKEN_MONTH.equals(tc)) {
  132. switch (token.length()) {
  133. case 1:
  134. response = Integer.toString(date.getMonth() + 1);
  135. break;
  136. case 2:
  137. response = twoCharDateField(date.getMonth() + 1);
  138. break;
  139. case 3:
  140. response = locale.MONTH_SHORT[date.getMonth()];
  141. break;
  142. default:
  143. response = locale.MONTH_LONG[date.getMonth()];
  144. break;
  145. }
  146. } else if (DateLocale.TOKEN_YEAR.equals(tc)) {
  147. if (token.length() >= 2)
  148. response = Integer.toString(date.getYear() + 1900);
  149. else
  150. response = twoCharDateField(date.getYear());
  151. } else if (DateLocale.TOKEN_HOUR_12.equals(tc)) {
  152. int h = date.getHours();
  153. if (h == 0)
  154. h = 12;
  155. else if (h > 12)
  156. h -= 12;
  157. // if (token.length() > 1)
  158. response = twoCharDateField(h);
  159. // else
  160. // response = Integer.toString(h);
  161. } else if (DateLocale.TOKEN_HOUR_24.equals(tc)) {
  162. // if (token.length() > 1)
  163. response = twoCharDateField(date.getHours());
  164. // else
  165. // response = Integer.toString(date.getHours());
  166. } else if (DateLocale.TOKEN_MINUTE.equals(tc)) {
  167. // if (token.length() > 1)
  168. response = twoCharDateField(date.getMinutes());
  169. // else
  170. // response = Integer.toString(date.getMinutes());
  171. } else if (DateLocale.TOKEN_SECOND.equals(tc)) {
  172. // if (token.length() > 1)
  173. response = twoCharDateField(date.getSeconds());
  174. // else
  175. // response = Integer.toString(date.getSeconds());
  176. } else if (DateLocale.TOKEN_AM_PM.equals(tc)) {
  177. int hour = date.getHours();
  178. if (hour > 11)
  179. response = DateLocale.getPM();
  180. else
  181. response = DateLocale.getAM();
  182. }
  183. return response;
  184. }
  185. /**
  186. * This is basically just a sneaky way to guarantee that our 1 or 2 digit
  187. * numbers come out as a 2 character string. we add an arbitrary number
  188. * larger than 100, convert this new number to a string, then take the right
  189. * most 2 characters.
  190. *
  191. * @param num
  192. * @return
  193. */
  194. private String twoCharDateField(int num) {
  195. String res = Integer.toString(num + 1900);
  196. res = res.substring(res.length() - 2);
  197. return res;
  198. }
  199. private static Date newDate(long time) {
  200. return new Date(time);
  201. }
  202. /**
  203. * Parses text and returns the corresponding date object.
  204. *
  205. * @param source
  206. * @return java.util.Date
  207. */
  208. public Date parse(String source) {
  209. return SimpleDateParser.parse(source, format);
  210. };
  211. }