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.

DefaultDateFunctions.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. Copyright (c) 2017 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl.expr;
  14. import java.time.DateTimeException;
  15. import java.time.DayOfWeek;
  16. import java.time.LocalDate;
  17. import java.time.LocalDateTime;
  18. import java.time.LocalTime;
  19. import java.time.Month;
  20. import java.time.MonthDay;
  21. import java.time.Year;
  22. import java.time.format.DateTimeFormatter;
  23. import java.time.format.TextStyle;
  24. import java.time.temporal.ChronoField;
  25. import java.time.temporal.ChronoUnit;
  26. import java.time.temporal.TemporalAccessor;
  27. import java.time.temporal.WeekFields;
  28. import com.healthmarketscience.jackcess.expr.EvalContext;
  29. import com.healthmarketscience.jackcess.expr.EvalException;
  30. import com.healthmarketscience.jackcess.expr.Function;
  31. import com.healthmarketscience.jackcess.expr.LocaleContext;
  32. import com.healthmarketscience.jackcess.expr.TemporalConfig;
  33. import com.healthmarketscience.jackcess.expr.Value;
  34. import com.healthmarketscience.jackcess.impl.ColumnImpl;
  35. import static com.healthmarketscience.jackcess.impl.expr.DefaultFunctions.*;
  36. import static com.healthmarketscience.jackcess.impl.expr.FunctionSupport.*;
  37. /**
  38. *
  39. * @author James Ahlborn
  40. */
  41. public class DefaultDateFunctions
  42. {
  43. // min, valid, recognizable date: January 1, 100 A.D. 00:00:00
  44. private static final double MIN_DATE = -657434.0d;
  45. // max, valid, recognizable date: December 31, 9999 A.D. 23:59:59
  46. private static final double MAX_DATE = 2958465.999988426d;
  47. private static final String INTV_YEAR = "yyyy";
  48. private static final String INTV_QUARTER = "q";
  49. private static final String INTV_MONTH = "m";
  50. private static final String INTV_DAY_OF_YEAR = "y";
  51. private static final String INTV_DAY = "d";
  52. private static final String INTV_WEEKDAY = "w";
  53. private static final String INTV_WEEK = "ww";
  54. private static final String INTV_HOUR = "h";
  55. private static final String INTV_MINUTE = "n";
  56. private static final String INTV_SECOND = "s";
  57. private static final WeekFields SUNDAY_FIRST =
  58. WeekFields.of(DayOfWeek.SUNDAY, 1);
  59. private DefaultDateFunctions() {}
  60. static void init() {
  61. // dummy method to ensure this class is loaded
  62. }
  63. public static final Function DATE = registerFunc(new Func0("Date") {
  64. @Override
  65. protected Value eval0(EvalContext ctx) {
  66. return ValueSupport.toValue(LocalDate.now());
  67. }
  68. });
  69. public static final Function DATEVALUE = registerFunc(new Func1NullIsNull("DateValue") {
  70. @Override
  71. protected Value eval1(EvalContext ctx, Value param1) {
  72. Value dv = param1.getAsDateTimeValue(ctx);
  73. if(dv.getType() == Value.Type.DATE) {
  74. return dv;
  75. }
  76. return ValueSupport.toValue(dv.getAsLocalDateTime(ctx).toLocalDate());
  77. }
  78. });
  79. public static final Function DATESERIAL = registerFunc(new Func3("DateSerial") {
  80. @Override
  81. protected Value eval3(EvalContext ctx, Value param1, Value param2, Value param3) {
  82. int year = param1.getAsLongInt(ctx);
  83. int month = param2.getAsLongInt(ctx);
  84. int day = param3.getAsLongInt(ctx);
  85. // "default" two digit year handling
  86. if(year < 100) {
  87. year += ((year <= 29) ? 2000 : 1900);
  88. }
  89. // we have to construct incrementatlly to handle out of range values
  90. LocalDate ld = LocalDate.of(year,1,1).plusMonths(month - 1)
  91. .plusDays(day - 1);
  92. return ValueSupport.toValue(ld);
  93. }
  94. });
  95. public static final Function DATEPART = registerFunc(new FuncVar("DatePart", 2, 4) {
  96. @Override
  97. protected Value evalVar(EvalContext ctx, Value[] params) {
  98. Value param2 = params[1];
  99. if(param2.isNull()) {
  100. return ValueSupport.NULL_VAL;
  101. }
  102. int firstDay = getFirstDayParam(ctx, params, 2);
  103. int firstWeekType = getFirstWeekTypeParam(ctx, params, 3);
  104. String intv = params[0].getAsString(ctx).trim();
  105. int result = -1;
  106. if(intv.equalsIgnoreCase(INTV_YEAR)) {
  107. result = param2.getAsLocalDateTime(ctx).getYear();
  108. } else if(intv.equalsIgnoreCase(INTV_QUARTER)) {
  109. result = getQuarter(param2.getAsLocalDateTime(ctx));
  110. } else if(intv.equalsIgnoreCase(INTV_MONTH)) {
  111. result = param2.getAsLocalDateTime(ctx).getMonthValue();
  112. } else if(intv.equalsIgnoreCase(INTV_DAY_OF_YEAR)) {
  113. result = param2.getAsLocalDateTime(ctx).getDayOfYear();
  114. } else if(intv.equalsIgnoreCase(INTV_DAY)) {
  115. result = param2.getAsLocalDateTime(ctx).getDayOfMonth();
  116. } else if(intv.equalsIgnoreCase(INTV_WEEKDAY)) {
  117. int dayOfWeek = param2.getAsLocalDateTime(ctx)
  118. .get(SUNDAY_FIRST.dayOfWeek());
  119. result = dayOfWeekToWeekDay(dayOfWeek, firstDay);
  120. } else if(intv.equalsIgnoreCase(INTV_WEEK)) {
  121. result = weekOfYear(ctx, param2, firstDay, firstWeekType);
  122. } else if(intv.equalsIgnoreCase(INTV_HOUR)) {
  123. result = param2.getAsLocalDateTime(ctx).getHour();
  124. } else if(intv.equalsIgnoreCase(INTV_MINUTE)) {
  125. result = param2.getAsLocalDateTime(ctx).getMinute();
  126. } else if(intv.equalsIgnoreCase(INTV_SECOND)) {
  127. result = param2.getAsLocalDateTime(ctx).getSecond();
  128. } else {
  129. throw new EvalException("Invalid interval " + intv);
  130. }
  131. return ValueSupport.toValue(result);
  132. }
  133. });
  134. public static final Function DATEADD = registerFunc(new Func3("DateAdd") {
  135. @Override
  136. protected Value eval3(EvalContext ctx,
  137. Value param1, Value param2, Value param3) {
  138. if(param3.isNull()) {
  139. return ValueSupport.NULL_VAL;
  140. }
  141. String intv = param1.getAsString(ctx).trim();
  142. int val = param2.getAsLongInt(ctx);
  143. LocalDateTime ldt = param3.getAsLocalDateTime(ctx);
  144. if(intv.equalsIgnoreCase(INTV_YEAR)) {
  145. ldt = ldt.plus(val, ChronoUnit.YEARS);
  146. } else if(intv.equalsIgnoreCase(INTV_QUARTER)) {
  147. ldt = ldt.plus(val * 3, ChronoUnit.MONTHS);
  148. } else if(intv.equalsIgnoreCase(INTV_MONTH)) {
  149. ldt = ldt.plus(val, ChronoUnit.MONTHS);
  150. } else if(intv.equalsIgnoreCase(INTV_DAY_OF_YEAR) ||
  151. intv.equalsIgnoreCase(INTV_DAY) ||
  152. intv.equalsIgnoreCase(INTV_WEEKDAY)) {
  153. ldt = ldt.plus(val, ChronoUnit.DAYS);
  154. } else if(intv.equalsIgnoreCase(INTV_WEEK)) {
  155. ldt = ldt.plus(val, ChronoUnit.WEEKS);
  156. } else if(intv.equalsIgnoreCase(INTV_HOUR)) {
  157. ldt = ldt.plus(val, ChronoUnit.HOURS);
  158. } else if(intv.equalsIgnoreCase(INTV_MINUTE)) {
  159. ldt = ldt.plus(val, ChronoUnit.MINUTES);
  160. } else if(intv.equalsIgnoreCase(INTV_SECOND)) {
  161. ldt = ldt.plus(val, ChronoUnit.SECONDS);
  162. } else {
  163. throw new EvalException("Invalid interval " + intv);
  164. }
  165. return ValueSupport.toValue(ldt);
  166. }
  167. });
  168. public static final Function DATEDIFF = registerFunc(new FuncVar("DateDiff", 3, 5) {
  169. @Override
  170. protected Value evalVar(EvalContext ctx, Value[] params) {
  171. Value param2 = params[1];
  172. Value param3 = params[2];
  173. if(param2.isNull() || param3.isNull()) {
  174. return ValueSupport.NULL_VAL;
  175. }
  176. int firstDay = getFirstDayParam(ctx, params, 3);
  177. int firstWeekType = getFirstWeekTypeParam(ctx, params, 4);
  178. String intv = params[0].getAsString(ctx).trim();
  179. LocalDateTime ldt1 = param2.getAsLocalDateTime(ctx);
  180. LocalDateTime ldt2 = param3.getAsLocalDateTime(ctx);
  181. int sign = 1;
  182. if(ldt1.isAfter(ldt2)) {
  183. LocalDateTime tmp = ldt1;
  184. ldt1 = ldt2;
  185. ldt2 = tmp;
  186. sign = -1;
  187. }
  188. // NOTE: DateDiff understands leap years, but not daylight savings time
  189. // (i.e. it doesn't really take into account timezones). so all time
  190. // based calculations assume 24 hour days.
  191. int result = -1;
  192. if(intv.equalsIgnoreCase(INTV_YEAR)) {
  193. result = ldt2.getYear() - ldt1.getYear();
  194. } else if(intv.equalsIgnoreCase(INTV_QUARTER)) {
  195. int y1 = ldt1.getYear();
  196. int q1 = getQuarter(ldt1);
  197. int y2 = ldt2.getYear();
  198. int q2 = getQuarter(ldt2);
  199. while(y2 > y1) {
  200. q2 += 4;
  201. --y2;
  202. }
  203. result = q2 - q1;
  204. } else if(intv.equalsIgnoreCase(INTV_MONTH)) {
  205. int y1 = ldt1.getYear();
  206. int m1 = ldt1.getMonthValue();
  207. int y2 = ldt2.getYear();
  208. int m2 = ldt2.getMonthValue();
  209. while(y2 > y1) {
  210. m2 += 12;
  211. --y2;
  212. }
  213. result = m2 - m1;
  214. } else if(intv.equalsIgnoreCase(INTV_DAY_OF_YEAR) ||
  215. intv.equalsIgnoreCase(INTV_DAY)) {
  216. result = getDayDiff(ldt1, ldt2);
  217. } else if(intv.equalsIgnoreCase(INTV_WEEKDAY)) {
  218. // this calulates number of 7 day periods between two dates
  219. result = getDayDiff(ldt1, ldt2) / 7;
  220. } else if(intv.equalsIgnoreCase(INTV_WEEK)) {
  221. // this counts number of "week of year" intervals between two dates
  222. WeekFields weekFields = weekFields(firstDay, firstWeekType);
  223. int w1 = ldt1.get(weekFields.weekOfWeekBasedYear());
  224. int y1 = ldt1.get(weekFields.weekBasedYear());
  225. int w2 = ldt2.get(weekFields.weekOfWeekBasedYear());
  226. int y2 = ldt2.get(weekFields.weekBasedYear());
  227. while(y2 > y1) {
  228. --y2;
  229. w2 += weeksInYear(y2, weekFields);
  230. }
  231. result = w2 - w1;
  232. } else if(intv.equalsIgnoreCase(INTV_HOUR)) {
  233. result = getHourDiff(ldt1, ldt2);
  234. } else if(intv.equalsIgnoreCase(INTV_MINUTE)) {
  235. result = getMinuteDiff(ldt1, ldt2);
  236. } else if(intv.equalsIgnoreCase(INTV_SECOND)) {
  237. int s1 = ldt1.getSecond();
  238. int s2 = ldt2.getSecond();
  239. int minuteDiff = getMinuteDiff(ldt1, ldt2);
  240. result = (s2 + (60 * minuteDiff)) - s1;
  241. } else {
  242. throw new EvalException("Invalid interval " + intv);
  243. }
  244. return ValueSupport.toValue(result * sign);
  245. }
  246. });
  247. public static final Function NOW = registerFunc(new Func0("Now") {
  248. @Override
  249. protected Value eval0(EvalContext ctx) {
  250. return ValueSupport.toValue(Value.Type.DATE_TIME,
  251. LocalDateTime.now(ctx.getZoneId()));
  252. }
  253. });
  254. public static final Function TIME = registerFunc(new Func0("Time") {
  255. @Override
  256. protected Value eval0(EvalContext ctx) {
  257. return ValueSupport.toValue(LocalTime.now(ctx.getZoneId()));
  258. }
  259. });
  260. public static final Function TIMEVALUE = registerFunc(new Func1NullIsNull("TimeValue") {
  261. @Override
  262. protected Value eval1(EvalContext ctx, Value param1) {
  263. Value dv = param1.getAsDateTimeValue(ctx);
  264. if(dv.getType() == Value.Type.TIME) {
  265. return dv;
  266. }
  267. return ValueSupport.toValue(dv.getAsLocalDateTime(ctx).toLocalTime());
  268. }
  269. });
  270. public static final Function TIMER = registerFunc(new Func0("Timer") {
  271. @Override
  272. protected Value eval0(EvalContext ctx) {
  273. double dd = LocalTime.now(ctx.getZoneId())
  274. .get(ChronoField.MILLI_OF_DAY) / 1000d;
  275. return ValueSupport.toValue(dd);
  276. }
  277. });
  278. public static final Function TIMESERIAL = registerFunc(new Func3("TimeSerial") {
  279. @Override
  280. protected Value eval3(EvalContext ctx, Value param1, Value param2, Value param3) {
  281. int hours = param1.getAsLongInt(ctx);
  282. int minutes = param2.getAsLongInt(ctx);
  283. int seconds = param3.getAsLongInt(ctx);
  284. // we have to construct incrementatlly to handle out of range values
  285. LocalTime lt = ColumnImpl.BASE_LT.plusHours(hours).plusMinutes(minutes)
  286. .plusSeconds(seconds);
  287. return ValueSupport.toValue(lt);
  288. }
  289. });
  290. public static final Function HOUR = registerFunc(new Func1NullIsNull("Hour") {
  291. @Override
  292. protected Value eval1(EvalContext ctx, Value param1) {
  293. return ValueSupport.toValue(param1.getAsLocalDateTime(ctx).getHour());
  294. }
  295. });
  296. public static final Function MINUTE = registerFunc(new Func1NullIsNull("Minute") {
  297. @Override
  298. protected Value eval1(EvalContext ctx, Value param1) {
  299. return ValueSupport.toValue(param1.getAsLocalDateTime(ctx).getMinute());
  300. }
  301. });
  302. public static final Function SECOND = registerFunc(new Func1NullIsNull("Second") {
  303. @Override
  304. protected Value eval1(EvalContext ctx, Value param1) {
  305. return ValueSupport.toValue(param1.getAsLocalDateTime(ctx).getSecond());
  306. }
  307. });
  308. public static final Function YEAR = registerFunc(new Func1NullIsNull("Year") {
  309. @Override
  310. protected Value eval1(EvalContext ctx, Value param1) {
  311. return ValueSupport.toValue(param1.getAsLocalDateTime(ctx).getYear());
  312. }
  313. });
  314. public static final Function MONTH = registerFunc(new Func1NullIsNull("Month") {
  315. @Override
  316. protected Value eval1(EvalContext ctx, Value param1) {
  317. return ValueSupport.toValue(param1.getAsLocalDateTime(ctx).getMonthValue());
  318. }
  319. });
  320. public static final Function MONTHNAME = registerFunc(new FuncVar("MonthName", 1, 2) {
  321. @Override
  322. protected Value evalVar(EvalContext ctx, Value[] params) {
  323. Value param1 = params[0];
  324. if(param1.isNull()) {
  325. return ValueSupport.NULL_VAL;
  326. }
  327. Month month = Month.of(param1.getAsLongInt(ctx));
  328. TextStyle textStyle = getTextStyle(ctx, params, 1);
  329. String monthName = month.getDisplayName(
  330. textStyle, ctx.getTemporalConfig().getLocale());
  331. return ValueSupport.toValue(monthName);
  332. }
  333. });
  334. public static final Function DAY = registerFunc(new Func1NullIsNull("Day") {
  335. @Override
  336. protected Value eval1(EvalContext ctx, Value param1) {
  337. return ValueSupport.toValue(
  338. param1.getAsLocalDateTime(ctx).getDayOfMonth());
  339. }
  340. });
  341. public static final Function WEEKDAY = registerFunc(new FuncVar("Weekday", 1, 2) {
  342. @Override
  343. protected Value evalVar(EvalContext ctx, Value[] params) {
  344. Value param1 = params[0];
  345. if(param1.isNull()) {
  346. return ValueSupport.NULL_VAL;
  347. }
  348. int dayOfWeek = param1.getAsLocalDateTime(ctx)
  349. .get(SUNDAY_FIRST.dayOfWeek());
  350. int firstDay = getFirstDayParam(ctx, params, 1);
  351. return ValueSupport.toValue(dayOfWeekToWeekDay(dayOfWeek, firstDay));
  352. }
  353. });
  354. public static final Function WEEKDAYNAME = registerFunc(new FuncVar("WeekdayName", 1, 3) {
  355. @Override
  356. protected Value evalVar(EvalContext ctx, Value[] params) {
  357. Value param1 = params[0];
  358. if(param1.isNull()) {
  359. return ValueSupport.NULL_VAL;
  360. }
  361. int weekday = param1.getAsLongInt(ctx);
  362. TextStyle textStyle = getTextStyle(ctx, params, 1);
  363. int firstDay = getFirstDayParam(ctx, params, 2);
  364. int dayOfWeek = weekDayToDayOfWeek(weekday, firstDay);
  365. String weekdayName = dayOfWeek(dayOfWeek).getDisplayName(
  366. textStyle, ctx.getTemporalConfig().getLocale());
  367. return ValueSupport.toValue(weekdayName);
  368. }
  369. });
  370. static Value stringToDateValue(LocaleContext ctx, String valStr) {
  371. // see if we can coerce to date/time
  372. TemporalConfig.Type valTempType = ExpressionTokenizer.determineDateType(
  373. valStr, ctx);
  374. if(valTempType != null) {
  375. DateTimeFormatter parseDf = ctx.createDateFormatter(
  376. ctx.getTemporalConfig().getDateTimeFormat(valTempType));
  377. try {
  378. TemporalAccessor parsedInfo = parseDf.parse(valStr);
  379. LocalDate ld = ColumnImpl.BASE_LD;
  380. if(valTempType.includesDate()) {
  381. // the year may not be explicitly specified
  382. if(parsedInfo.isSupported(ChronoField.YEAR)) {
  383. ld = LocalDate.from(parsedInfo);
  384. } else {
  385. ld = MonthDay.from(parsedInfo).atYear(
  386. Year.now(ctx.getZoneId()).getValue());
  387. }
  388. }
  389. LocalTime lt = ColumnImpl.BASE_LT;
  390. if(valTempType.includesTime()) {
  391. lt = LocalTime.from(parsedInfo);
  392. }
  393. return ValueSupport.toValue(LocalDateTime.of(ld, lt));
  394. } catch(DateTimeException de) {
  395. // note a valid date/time
  396. }
  397. }
  398. // not a valid date string, not a date/time
  399. return null;
  400. }
  401. static boolean isValidDateDouble(double dd) {
  402. return ((dd >= MIN_DATE) && (dd <= MAX_DATE));
  403. }
  404. static Value numberToDateValue(double dd) {
  405. if(!isValidDateDouble(dd)) {
  406. // outside valid date range
  407. return null;
  408. }
  409. LocalDateTime ldt = ColumnImpl.ldtFromLocalDateDouble(dd);
  410. return ValueSupport.toValue(ldt);
  411. }
  412. private static int dayOfWeekToWeekDay(int day, int firstDay) {
  413. // shift all the values to 0 based to calculate the correct value, then
  414. // back to 1 based to return the result
  415. return (((day - 1) - (firstDay - 1) + 7) % 7) + 1;
  416. }
  417. private static int weekDayToDayOfWeek(int weekday, int firstDay) {
  418. // shift all the values to 0 based to calculate the correct value, then
  419. // back to 1 based to return the result
  420. return (((firstDay - 1) + (weekday - 1)) % 7) + 1;
  421. }
  422. static int getFirstDayParam(
  423. LocaleContext ctx, Value[] params, int idx) {
  424. // vbSunday (default) 1
  425. // vbUseSystem 0
  426. return getOptionalIntParam(ctx, params, idx, 1, 0);
  427. }
  428. static int getFirstWeekTypeParam(
  429. LocaleContext ctx, Value[] params, int idx) {
  430. // vbFirstJan1 (default) 1
  431. // vbUseSystem 0
  432. return getOptionalIntParam(ctx, params, idx, 1, 0);
  433. }
  434. static WeekFields weekFields(int firstDay, int firstWeekType) {
  435. int minDays = 1;
  436. switch(firstWeekType) {
  437. case 1:
  438. // vbUseSystem 0
  439. // vbFirstJan1 1 (default)
  440. break;
  441. case 2:
  442. // vbFirstFourDays 2
  443. minDays = 4;
  444. break;
  445. case 3:
  446. // vbFirstFullWeek 3
  447. minDays = 7;
  448. break;
  449. default:
  450. throw new EvalException("Invalid first week of year type " +
  451. firstWeekType);
  452. }
  453. return WeekFields.of(dayOfWeek(firstDay), minDays);
  454. }
  455. private static DayOfWeek dayOfWeek(int dayOfWeek) {
  456. return DayOfWeek.SUNDAY.plus(dayOfWeek - 1);
  457. }
  458. private static TextStyle getTextStyle(EvalContext ctx, Value[] params,
  459. int idx) {
  460. boolean abbreviate = getOptionalBooleanParam(ctx, params, 1);
  461. return (abbreviate ? TextStyle.SHORT : TextStyle.FULL);
  462. }
  463. private static int weekOfYear(EvalContext ctx, Value param, int firstDay,
  464. int firstWeekType) {
  465. return weekOfYear(param.getAsLocalDateTime(ctx), firstDay, firstWeekType);
  466. }
  467. private static int weekOfYear(LocalDateTime ldt, int firstDay,
  468. int firstWeekType) {
  469. WeekFields weekFields = weekFields(firstDay, firstWeekType);
  470. return ldt.get(weekFields.weekOfWeekBasedYear());
  471. }
  472. private static int weeksInYear(int year, WeekFields weekFields) {
  473. return (int)LocalDate.of(year,2,1).range(weekFields.weekOfWeekBasedYear())
  474. .getMaximum();
  475. }
  476. private static int getQuarter(LocalDateTime ldt) {
  477. int month = ldt.getMonthValue() - 1;
  478. return (month / 3) + 1;
  479. }
  480. private static int getDayDiff(LocalDateTime ldt1, LocalDateTime ldt2) {
  481. int y1 = ldt1.getYear();
  482. int d1 = ldt1.getDayOfYear();
  483. int y2 = ldt2.getYear();
  484. int d2 = ldt2.getDayOfYear();
  485. while(y2 > y1) {
  486. ldt2 = ldt2.minusYears(1);
  487. d2 += ldt2.range(ChronoField.DAY_OF_YEAR).getMaximum();
  488. y2 = ldt2.getYear();
  489. }
  490. return d2 - d1;
  491. }
  492. private static int getHourDiff(LocalDateTime ldt1, LocalDateTime ldt2) {
  493. int h1 = ldt1.getHour();
  494. int h2 = ldt2.getHour();
  495. int dayDiff = getDayDiff(ldt1, ldt2);
  496. return (h2 + (24 * dayDiff)) - h1;
  497. }
  498. private static int getMinuteDiff(LocalDateTime ldt1, LocalDateTime ldt2) {
  499. int m1 = ldt1.getMinute();
  500. int m2 = ldt2.getMinute();
  501. int hourDiff = getHourDiff(ldt1, ldt2);
  502. return (m2 + (60 * hourDiff)) - m1;
  503. }
  504. }