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.

DateTimeService.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client;
  5. import java.util.Date;
  6. import com.vaadin.terminal.gwt.client.ui.VDateField;
  7. /**
  8. * This class provides date/time parsing services to all components on the
  9. * client side.
  10. *
  11. * @author IT Mill Ltd.
  12. *
  13. */
  14. @SuppressWarnings("deprecation")
  15. public class DateTimeService {
  16. private String currentLocale;
  17. private static int[] maxDaysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
  18. 31, 30, 31 };
  19. /**
  20. * Creates a new date time service with the application default locale.
  21. */
  22. public DateTimeService() {
  23. currentLocale = LocaleService.getDefaultLocale();
  24. }
  25. /**
  26. * Creates a new date time service with a given locale.
  27. *
  28. * @param locale
  29. * e.g. fi, en etc.
  30. * @throws LocaleNotLoadedException
  31. */
  32. public DateTimeService(String locale) throws LocaleNotLoadedException {
  33. setLocale(locale);
  34. }
  35. public void setLocale(String locale) throws LocaleNotLoadedException {
  36. if (LocaleService.getAvailableLocales().contains(locale)) {
  37. currentLocale = locale;
  38. } else {
  39. throw new LocaleNotLoadedException(locale);
  40. }
  41. }
  42. public String getLocale() {
  43. return currentLocale;
  44. }
  45. public String getMonth(int month) {
  46. try {
  47. return LocaleService.getMonthNames(currentLocale)[month];
  48. } catch (final LocaleNotLoadedException e) {
  49. ClientExceptionHandler.displayError(e);
  50. }
  51. return null;
  52. }
  53. public String getShortMonth(int month) {
  54. try {
  55. return LocaleService.getShortMonthNames(currentLocale)[month];
  56. } catch (final LocaleNotLoadedException e) {
  57. ClientExceptionHandler.displayError(e);
  58. }
  59. return null;
  60. }
  61. public String getDay(int day) {
  62. try {
  63. return LocaleService.getDayNames(currentLocale)[day];
  64. } catch (final LocaleNotLoadedException e) {
  65. ClientExceptionHandler.displayError(e);
  66. }
  67. return null;
  68. }
  69. public String getShortDay(int day) {
  70. try {
  71. return LocaleService.getShortDayNames(currentLocale)[day];
  72. } catch (final LocaleNotLoadedException e) {
  73. ClientExceptionHandler.displayError(e);
  74. }
  75. return null;
  76. }
  77. public int getFirstDayOfWeek() {
  78. try {
  79. return LocaleService.getFirstDayOfWeek(currentLocale);
  80. } catch (final LocaleNotLoadedException e) {
  81. ClientExceptionHandler.displayError(e);
  82. }
  83. return 0;
  84. }
  85. public boolean isTwelveHourClock() {
  86. try {
  87. return LocaleService.isTwelveHourClock(currentLocale);
  88. } catch (final LocaleNotLoadedException e) {
  89. ClientExceptionHandler.displayError(e);
  90. }
  91. return false;
  92. }
  93. public String getClockDelimeter() {
  94. try {
  95. return LocaleService.getClockDelimiter(currentLocale);
  96. } catch (final LocaleNotLoadedException e) {
  97. ClientExceptionHandler.displayError(e);
  98. }
  99. return ":";
  100. }
  101. public String[] getAmPmStrings() {
  102. try {
  103. return LocaleService.getAmPmStrings(currentLocale);
  104. } catch (final LocaleNotLoadedException e) {
  105. ClientExceptionHandler.displayError(e);
  106. }
  107. final String[] temp = new String[2];
  108. temp[0] = "AM";
  109. temp[1] = "PM";
  110. return temp;
  111. }
  112. public int getStartWeekDay(Date date) {
  113. final Date dateForFirstOfThisMonth = new Date(date.getYear(),
  114. date.getMonth(), 1);
  115. int firstDay;
  116. try {
  117. firstDay = LocaleService.getFirstDayOfWeek(currentLocale);
  118. } catch (final LocaleNotLoadedException e) {
  119. firstDay = 0;
  120. ClientExceptionHandler.displayError(e);
  121. }
  122. int start = dateForFirstOfThisMonth.getDay() - firstDay;
  123. if (start < 0) {
  124. start = 6;
  125. }
  126. return start;
  127. }
  128. public static void setMilliseconds(Date date, int ms) {
  129. date.setTime(date.getTime() / 1000 * 1000 + ms);
  130. }
  131. public static int getMilliseconds(Date date) {
  132. if (date == null) {
  133. return 0;
  134. }
  135. return (int) (date.getTime() - date.getTime() / 1000 * 1000);
  136. }
  137. public static int getNumberOfDaysInMonth(Date date) {
  138. final int month = date.getMonth();
  139. if (month == 1 && true == isLeapYear(date)) {
  140. return 29;
  141. }
  142. return maxDaysInMonth[month];
  143. }
  144. public static boolean isLeapYear(Date date) {
  145. // Instantiate the date for 1st March of that year
  146. final Date firstMarch = new Date(date.getYear(), 2, 1);
  147. // Go back 1 day
  148. final long firstMarchTime = firstMarch.getTime();
  149. final long lastDayTimeFeb = firstMarchTime - (24 * 60 * 60 * 1000); // NUM_MILLISECS_A_DAY
  150. // Instantiate new Date with this time
  151. final Date febLastDay = new Date(lastDayTimeFeb);
  152. // Check for date in this new instance
  153. return (29 == febLastDay.getDate()) ? true : false;
  154. }
  155. public static boolean isSameDay(Date d1, Date d2) {
  156. return (getDayInt(d1) == getDayInt(d2));
  157. }
  158. public static boolean isInRange(Date date, Date rangeStart, Date rangeEnd,
  159. int resolution) {
  160. Date s;
  161. Date e;
  162. if (rangeStart.after(rangeEnd)) {
  163. s = rangeEnd;
  164. e = rangeStart;
  165. } else {
  166. e = rangeEnd;
  167. s = rangeStart;
  168. }
  169. long start = s.getYear() * 10000000000l;
  170. long end = e.getYear() * 10000000000l;
  171. long target = date.getYear() * 10000000000l;
  172. if (resolution == VDateField.RESOLUTION_YEAR) {
  173. return (start <= target && end >= target);
  174. }
  175. start += s.getMonth() * 100000000l;
  176. end += e.getMonth() * 100000000l;
  177. target += date.getMonth() * 100000000l;
  178. if (resolution == VDateField.RESOLUTION_MONTH) {
  179. return (start <= target && end >= target);
  180. }
  181. start += s.getDate() * 1000000l;
  182. end += e.getDate() * 1000000l;
  183. target += date.getDate() * 1000000l;
  184. if (resolution == VDateField.RESOLUTION_DAY) {
  185. return (start <= target && end >= target);
  186. }
  187. start += s.getHours() * 10000l;
  188. end += e.getHours() * 10000l;
  189. target += date.getHours() * 10000l;
  190. if (resolution == VDateField.RESOLUTION_HOUR) {
  191. return (start <= target && end >= target);
  192. }
  193. start += s.getMinutes() * 100l;
  194. end += e.getMinutes() * 100l;
  195. target += date.getMinutes() * 100l;
  196. if (resolution == VDateField.RESOLUTION_MIN) {
  197. return (start <= target && end >= target);
  198. }
  199. start += s.getSeconds();
  200. end += e.getSeconds();
  201. target += date.getSeconds();
  202. return (start <= target && end >= target);
  203. }
  204. private static int getDayInt(Date date) {
  205. final int y = date.getYear();
  206. final int m = date.getMonth();
  207. final int d = date.getDate();
  208. return ((y + 1900) * 10000 + m * 100 + d) * 1000000000;
  209. }
  210. /**
  211. * Returns the ISO-8601 week number of the given date.
  212. *
  213. * @param date
  214. * The date for which the week number should be resolved
  215. * @return The ISO-8601 week number for {@literal date}
  216. */
  217. public static int getISOWeekNumber(Date date) {
  218. final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
  219. int dayOfWeek = date.getDay(); // 0 == sunday
  220. // ISO 8601 use weeks that start on monday so we use
  221. // mon=1,tue=2,...sun=7;
  222. if (dayOfWeek == 0) {
  223. dayOfWeek = 7;
  224. }
  225. // Find nearest thursday (defines the week in ISO 8601). The week number
  226. // for the nearest thursday is the same as for the target date.
  227. int nearestThursdayDiff = 4 - dayOfWeek; // 4 is thursday
  228. Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff
  229. * MILLISECONDS_PER_DAY);
  230. Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1);
  231. long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime();
  232. int daysSinceFirstOfJanuary = (int) (timeDiff / MILLISECONDS_PER_DAY);
  233. int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1;
  234. return weekNumber;
  235. }
  236. }