Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DateTimeService.java 6.6KB

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