void dateTest() throws RuntimeException { Locale locTH = new Locale("th", "TH", "TH"); TimeZone tz = TimeZone.getTimeZone("PST"); Calendar calGregorian = Calendar.getInstance(tz, Locale.US); calGregorian.clear(); calGregorian.set(2002, 4, 1, 8, 30); final Date date = calGregorian.getTime(); Calendar cal = Calendar.getInstance(tz, locTH); cal.clear(); cal.setTime(date); final String strExpected = "\u0E27\u0E31\u0E19\u0E1E\u0E38\u0E18\u0E17\u0E35\u0E48\u0020\u0E51\u0020\u0E1E\u0E24\u0E29\u0E20\u0E32\u0E04\u0E21\u0020\u0E1E\u002E\u0E28\u002E\u0020\u0E52\u0E55\u0E54\u0E55\u002C\u0020\u0E58\u0020\u0E19\u0E32\u0E2C\u0E34\u0E01\u0E32\u0020\u0E53\u0E50\u0020\u0E19\u0E32\u0E17\u0E35\u0020\u0E50\u0E50\u0020\u0E27\u0E34\u0E19\u0E32\u0E17\u0E35"; Date value = cal.getTime(); // th_TH_TH test DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locTH); df.setTimeZone(tz); String str = df.format(value); if (!strExpected.equals(str)) { throw new RuntimeException(); } }
private void doTest(String candidate, int expectedHours, int expectedMins) throws ParseException { DateFormat df = new SimpleDateFormat("HH:mm"); Date result = df.parse(candidate); Calendar cal = Calendar.getInstance(); cal.setTime(result); assertEquals(expectedHours, cal.get(Calendar.HOUR_OF_DAY)); assertEquals(expectedMins, cal.get(Calendar.MINUTE)); }
/** * Finds the next occurrence for monthly recurrence. * * @param startDate the start date of the previous calendar item. * @param endDate the end date of the previous calendar item. * @param lastDay if <tt>true</tt> we are interested in last day of the month * @return the next item */ public CalendarItemTimerTask nextMonth(Date startDate, Date endDate, boolean lastDay) { long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal = incrementMonths(cal, lastDay, period); Date currentDate = new Date(); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(currentDate); int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR); int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH)); int monthsToAdd = months; monthsToAdd -= months % period; cal = incrementMonths(cal, lastDay, monthsToAdd); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { cal = incrementMonths(cal, lastDay, period); } } Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal = incrementMonths(cal, lastDay, period); cal2 = incrementMonths(cal2, lastDay, period); } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) { return null; } boolean executeNow = false; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); }
public void testDate() { final Date date = Vba.date(); assertNotNull(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); assertEquals(0, calendar.get(Calendar.MILLISECOND)); }
public static String getZoneOffset(Date date, Locale locale) { String dateStr; Calendar cal = Calendar.getInstance(locale); cal.setTime(date); int offset = cal.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000); if (offset > -1) dateStr = " +" + offset + "h"; else dateStr = " -" + offset + "h"; return dateStr; }
public void testDateValue() { Date date = new Date(); final Date date1 = Vba.dateValue(date); Calendar calendar = Calendar.getInstance(); calendar.setTime(date1); assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); assertEquals(0, calendar.get(Calendar.MINUTE)); assertEquals(0, calendar.get(Calendar.SECOND)); assertEquals(0, calendar.get(Calendar.MILLISECOND)); }
/** * Checks whether the given date is in the recurrent pattern range or not * * @param date the date * @return <tt>true</tt> if the date is in the pattern range. */ private boolean dateOutOfRange(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); if ((endType != 0x00002023) && (endType != 0xFFFFFFFF) && cal.getTime().after(windowsTimeToDateObject(this.endDate))) { return true; // the series are finished } return false; }
/** * Converts a Date into a formatted string. * * @param date The Date to convert. * @return The formatted string. */ public static String format(Date date) { Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); StringBuffer sb = new StringBuffer(); c.setTime(date); sb.append(w4.format(c.get(Calendar.YEAR))); sb.append(w2.format(c.get(Calendar.MONTH) + 1)); sb.append(w2.format(c.get(Calendar.DAY_OF_MONTH))); sb.append(w2.format(c.get(Calendar.HOUR_OF_DAY))); sb.append(w2.format(c.get(Calendar.MINUTE))); sb.append(w2.format(c.get(Calendar.SECOND))); return sb.toString(); }
protected Calendar formatStringAsCalendar(String calendarString) { try { String dateOnly = calendarString.substring(0, 19); String timeZoneStr = calendarString.substring(20, calendarString.length()); TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); simpleDateFormat.setTimeZone(timeZone); Date referenceDate = simpleDateFormat.parse(dateOnly.toUpperCase()); Calendar referenceCalendar = new GregorianCalendar(timeZone); referenceCalendar.setTime(referenceDate); simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z"); simpleDateFormat.setTimeZone(timeZone); return referenceCalendar; } catch (ParseException p) { throw new TestErrorException("Could not parse Calendar String: " + calendarString); } }
static String formatDate(Date d) { Calendar c = new GregorianCalendar(TimeZone.getTimeZone("UTC")); StringBuffer sb = new StringBuffer(); NumberFormat w4 = new DecimalFormat(); w4.setMinimumIntegerDigits(4); w4.setGroupingUsed(false); NumberFormat w2 = new DecimalFormat(); w2.setMinimumIntegerDigits(2); c.setTime(d); sb.append(w4.format(c.get(c.YEAR))); sb.append(w2.format(c.get(c.MONTH) + 1)); sb.append(w2.format(c.get(c.DAY_OF_MONTH))); sb.append(w2.format(c.get(c.HOUR_OF_DAY))); sb.append(w2.format(c.get(c.MINUTE))); sb.append(w2.format(c.get(c.SECOND))); return sb.toString(); }
private void saveStartProjects(File file) throws IOException { Calendar cal = Calendar.getInstance(); try (PrintWriter out = new PrintWriter(file, "windows-1251")) { for (Region region : this.regions) { cal.setTime(region.start); out.write("\""); out.write(region.filial + " " + region.name); out.write("\";;;"); int skeepCells = (cal.get(Calendar.YEAR) - 2017) * 5 + (cal.get(Calendar.MONDAY) % 4); for (int i = 0; i < skeepCells; i++) { out.write(";"); } out.println("X"); } } }
/** * Finds the occurrence of the events in the next months * * @param startDate the start date if the calendar item * @param dayOfWeekInMonth the number of week days occurrences * @return the date of the next occurrence */ private Date getMonthNStartDate(Date startDate, int dayOfWeekInMonth) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate); if (dayOfWeekInMonth == -1) { Date result = null; cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth); for (int day : allowedDaysOfWeek) { cal.set(Calendar.DAY_OF_WEEK, day); if (result == null || result.before(cal.getTime())) result = cal.getTime(); } return result; } else while (dayOfWeekInMonth > 0) { int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); if (allowedDaysOfWeek.contains(dayOfWeek)) dayOfWeekInMonth--; if (dayOfWeekInMonth > 0) cal.add(Calendar.DAY_OF_MONTH, 1); } return cal.getTime(); }
/** * Map millis and timeZone to its component fields. * * <p>Bits0: ------------------------------------------------ Field Num Bits Range Loc * ------------------------------------------------ Year 16 short 16-31 Millis 16 short 0-15 * * <p>Bits1: ------------------------------------------------ Field Num Bits Range Loc * ------------------------------------------------ Daylight 1 0-1 29-29 Month 4 1-12 25-28 Day 5 * 1-31 20-24 Hour 5 0-23 15-19 Minutes 6 0-59 9-14 Seconds 6 0-59 3-8 Weekday 3 0-6 0-2 * ------------------------------------------------ */ private void millisToFields() { // init a calendar with timeZone and millis Calendar calendar = new GregorianCalendar(timeZone); Date date = new Date(millis); calendar.setTime(date); // set year bits int x = calendar.get(Calendar.YEAR); bits0 |= ((x & 0xFFFF) << 16); // set millisecond bits x = calendar.get(Calendar.MILLISECOND); bits0 |= ((x & 0xFFFF) << 0); // set month bits x = calendar.get(Calendar.MONTH) + 1; bits1 |= ((x & 0x0F) << 25); // set day bits x = calendar.get(Calendar.DAY_OF_MONTH); bits1 |= ((x & 0x1F) << 20); // set hour bits x = calendar.get(Calendar.HOUR_OF_DAY); bits1 |= ((x & 0x1F) << 15); // set minute bits x = calendar.get(Calendar.MINUTE); bits1 |= ((x & 0x3F) << 9); // set seconds bits x = calendar.get(Calendar.SECOND); bits1 |= ((x & 0x3F) << 3); // set weekday x = calendar.get(Calendar.DAY_OF_WEEK) - 1; bits1 |= ((x & 0x07) << 0); // set daylight bit if (timeZone.inDaylightTime(date)) bits1 |= (0x01 << 29); }
/** * Finds the next occurrence for monthly Nth recurrence. * * @param startDate the start date of the previous calendar item. * @param endDate the end date of the previous calendar item. * @return the next item */ public CalendarItemTimerTask nextMonthN(Date startDate, Date endDate) { int dayOfWeekInMonth = (patternSpecific2 == 5 ? -1 : patternSpecific2); long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); Date currentDate = new Date(); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(currentDate); int years = cal2.get(Calendar.YEAR) - cal.get(Calendar.YEAR); int months = (years * 12) + (cal2.get(Calendar.MONTH) - cal.get(Calendar.MONTH)); int monthsToAdd = months; monthsToAdd -= months % period; cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, monthsToAdd); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); if (cal.getTimeInMillis() + duration < currentDate.getTime()) { cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, monthsToAdd); cal.setTime(getMonthNStartDate(cal.getTime(), dayOfWeekInMonth)); } } Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); startDate = null; for (int dayOfWeek : allowedDaysOfWeek) { cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, dayOfWeekInMonth); if ((cal.after(startDate) && dayOfWeekInMonth == -1) || (cal.before(startDate) && dayOfWeekInMonth != -1) || startDate == null) { startDate = cal.getTime(); cal2.set(Calendar.YEAR, cal.get(Calendar.YEAR)); cal2.set(Calendar.MONTH, cal.get(Calendar.MONTH)); cal2.set(Calendar.DATE, cal.get(Calendar.DATE)); } } } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) return null; boolean executeNow = false; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); }
@Test public void testDirectLearning() throws Exception { final String symbols[] = new String[] {"BBVA.MC", "BKIA.MC", "BKT.MC", "BME.MC", "SAN.MC"}; final CalUtils.InstantGenerator instantGenerator = new LocalTimeMinutes(5); final double coreDailyVols[][] = new double[][] { new double[] { 55219, 262256, 202661, 218359, 178244, 99610, 92348, 124795, 214370, 153854, 204116, 173501, 85390, 156835, 108070, 23755, 118573, 70117, 55768, 52643, 71485, 407645, 442909, 129109, 188896, 79590, 422121, 290067, 227955, 69257, 41634, 446002, 579188, 86237, 1606794, 83676, 166393, 84987, 875905, 117013, 399084, 116190, 149507, 207221, 60857, 155612, 448006, 198637, 67695, 65423, 180038, 88774, 80273, 86065, 85231, 38867, 29330, 116353, 26887, 34170, 102518, 72246, 21274, 70752, 37912, 49367, 100472, 49461, 41735, 45795, 36578, 311945, 249039, 70487, 121906, 136424, 195136, 166308, 331734, 343180, 419616, 104613, 1354058, 162678, 141067, 147039, 149115, 271162, 543989, 184421, 340679, 201939, 293860, 171035, 263964, 260198, 428087, 565126, 385874, 547890, 384416, 256696, 0, 4738359 }, new double[] { 1298630, 678084, 488607, 224766, 434263, 356933, 576571, 219236, 252805, 414776, 166828, 174665, 146281, 110944, 145234, 179739, 253111, 175685, 64925, 216682, 494507, 100205, 67371, 101019, 158026, 316281, 334067, 954850, 115547, 163051, 130303, 107600, 1407996, 90357, 110452, 451866, 238004, 3096215, 2672803, 190170, 111282, 107135, 453389, 60821, 98292, 1310864, 1132267, 241907, 89915, 175676, 61621, 521553, 212388, 288651, 193578, 272161, 256777, 236382, 802159, 230248, 387068, 160647, 106999, 391933, 465080, 374577, 340378, 330708, 416320, 200347, 251986, 336664, 311970, 600559, 508011, 922379, 311581, 352459, 508727, 159316, 1355635, 246541, 389672, 805957, 370754, 382556, 316971, 564228, 437166, 277733, 1284505, 1763095, 169661, 280682, 969102, 540315, 451895, 308036, 715130, 642966, 981563, 900778, 0, 7155528 }, new double[] { 679280, 229518, 346536, 347215, 316025, 313890, 235844, 199995, 1920617, 129356, 172084, 207860, 317578, 10369008, 480990, 1403537, 1021730, 156125, 94833, 366987, 145687, 322957, 328120, 66657, 176001, 271003, 133121, 558624, 264638, 638663, 165080, 129439, 5126344, 5438632, 248806, 250616, 112716, 54523, 198097, 67772, 1414565, 244509, 246205, 151540, 98584, 51217, 94193, 111763, 104726, 45880, 64242, 78893, 60706, 48117, 133085, 101941, 5103803, 5084823, 168230, 75537, 815036, 73409, 422412, 437127, 115802, 326536, 54707, 81759, 94420, 208637, 50361, 1458556, 84257, 129114, 54632, 105873, 57165, 77578, 233302, 195560, 134194, 180928, 140433, 123154, 221422, 339866, 1343886, 114699, 170052, 150679, 181731, 160943, 192590, 125556, 132656, 154740, 320932, 140929, 117889, 381656, 393635, 306177, 0, 21629250 }, new double[] { 526909, 167180, 199570, 149154, 142141, 320881, 223750, 102275, 258400, 202197, 120202, 93404, 178631, 106401, 346186, 231729, 163656, 1622531, 125689, 2656587, 5336032, 2385985, 335692, 86118, 130551, 99047, 81695, 98846, 238413, 4831684, 293262, 124652, 106642, 112048, 14284646, 111209, 2204635, 128940, 83395, 134816, 116320, 65412, 165020, 126511, 92217, 111751, 47320, 82219, 19044177, 70827, 21676, 211214, 103108, 22771, 61629, 4816563, 63806, 33989, 130104, 146897, 15046441, 44977, 40889, 54584, 54591, 76634, 238536, 68583, 110591, 75012, 503760, 209479, 217929, 86397, 102284, 81878, 252785, 135884, 129149, 112760, 266851, 110863, 67866, 55205, 150165, 699438, 184450, 270270, 4270036, 345303, 895116, 217142, 145398, 301231, 10260595, 136317, 442910, 371357, 189023, 538928, 438973, 926728, 9137, 8879481 }, new double[] { 1318228, 1391326, 574558, 441739, 719144, 522626, 404351, 383602, 490710, 284952, 2984474, 216339, 10220195, 247067, 166223, 224310, 10181837, 126161, 9764418, 692337, 25907353, 1518741, 1179929, 120730, 10173292, 290045, 19824327, 402527, 277859, 3116841, 7164061, 332021, 10560006, 2334129, 121753, 200177, 246402, 10106648, 1137272, 2084673, 461849, 125108, 465907, 156972, 139083, 127389, 237263, 311691, 156536, 155322, 133368, 329715, 256088, 116835, 5192615, 823762, 183836, 1110239, 2414836, 385072, 599637, 387285, 291580, 2796924, 12977051, 338582, 884415, 525622, 322587, 223348, 668858, 143039, 627590, 239797, 232788, 256503, 209425, 375474, 558106, 290991, 1176648, 286550, 149539, 297435, 602136, 152733, 212363, 178992, 179644, 295428, 933636, 349405, 660749, 226061, 868852, 318539, 469303, 538061, 273643, 444084, 347730, 825808, 12011, 7792372 } }; // Complete data with not-open-market moments represented as NaN. final double[][] synthesizedDailyVols = new double[coreDailyVols.length][instantGenerator.maxInstantValue()]; final Calendar openTime = Calendar.getInstance(); final int n = 9 * 60 / 5; final int m = n + coreDailyVols[0].length; for (int i = 0; i < 5; i++) { for (int j = 0; j < n; j++) { synthesizedDailyVols[i][j] = Double.NaN; } for (int j = 0; j < coreDailyVols[i].length; j++) { synthesizedDailyVols[i][j + n] = coreDailyVols[i][j]; } for (int j = m; j < instantGenerator.maxInstantValue(); j++) { synthesizedDailyVols[i][j] = Double.NaN; } } final PredictorListener predictor = new PCAVolumeProfileConsideringMarketPredictor(instantGenerator, 0.0d, symbols); final Calendar moment = Calendar.getInstance(); moment.setTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2011-10-26 00:00:00")); for (int i = 0; i < synthesizedDailyVols[0].length; i++) { final double binData[] = new double[5]; final Calendar now = (Calendar) moment.clone(); for (int j = 0; j < 5; j++) { binData[j] = synthesizedDailyVols[j][i]; } predictor.learnVector(now, binData); moment.add(Calendar.MINUTE, 5); } final Calendar predictionMoment = Calendar.getInstance(); predictionMoment.setTime( new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2011-10-27 00:00:00")); final double prediction[] = predictor.predictVector(predictionMoment); double sum = 0; for (int i = 0; i < prediction.length; i++) { if (!Double.isNaN(prediction[i])) { sum += prediction[i]; } } // Check if prediction sums 100 assertEquals(sum, 100.0d, 0.000001d); // Check if there is no negative values for (int i = 0; i < prediction.length; i++) { if (!Double.isNaN(prediction[i])) { assertEquals(true, prediction[i] >= 0); } } // Check the whole prediction final double coreExpectedPrediction[] = new double[] { 1.9288035, 1.5177448, 0.9332691, 0.6917373, 0.9226087, 0.7836382, 0.7738424, 0.5111597, 1.1244135, 0.6359005, 0.5037461, 0.4346380, 0.4012883, 0.4283590, 0.5155573, 0.6262221, 0.6433344, 1.0639806, 0.1995134, 0.6614736, 0.7967144, 1.3838254, 1.3177505, 0.2612888, 0.3982873, 0.4867157, 0.7851480, 1.1897632, 0.5728564, 0.5297141, 0.4111288, 0.7186205, 1.6064740, 0.3591866, 0.2788936, 0.5225355, 0.4710717, 0.3902378, 1.6495111, 0.8969267, 1.0190033, 0.2823669, 0.7617028, 0.3711431, 0.2148944, 1.0168186, 1.2270160, 0.5254874, 0.2223704, 0.2518789, 0.2597858, 0.6560785, 0.3639917, 0.2926088, 0.2736937, 0.6651141, 0.2898492, 0.8638302, 1.1774701, 0.4286046, 0.8646737, 0.3718615, 0.3104019, 0.5365190, 0.4799254, 0.5199251, 0.8793190, 0.5422813, 0.4963591, 0.3227974, 0.7813186, 0.9484949, 0.8559482, 0.5814706, 0.5639950, 0.8369635, 0.5824984, 0.6162544, 0.9984222, 0.6363279, 1.9798710, 0.4541408, 0.4314029, 0.8058571, 0.7680111, 0.8377700, 0.7831125, 0.8088483, 1.0056747, 0.6680795, 2.06074038, 1.2474098, 0.8186405, 0.5901474, 1.4477580, 0.8018070, 1.1874030, 1.1958628, 1.0192618, 1.4630908, 1.4049921, 1.7359802, 0.0000000, 22.2667882 }; final double synthesizedExpectedPrediction[] = new double[instantGenerator.maxInstantValue()]; for (int j = 0; j < n; j++) { synthesizedExpectedPrediction[j] = Double.NaN; } for (int j = 0; j < coreExpectedPrediction.length; j++) { synthesizedExpectedPrediction[j + n] = coreExpectedPrediction[j]; } for (int j = m; j < instantGenerator.maxInstantValue(); j++) { synthesizedExpectedPrediction[j] = Double.NaN; } assertEquals(prediction.length, synthesizedExpectedPrediction.length); for (int i = 0; i < prediction.length; i++) { assertEquals(prediction[i], synthesizedExpectedPrediction[i], 0.0001d); } System.out.println("Ok"); }
/** * Calculates and creates the next calendar item. * * @param previousStartDate the start date of the previous occurrence. * @param previousEndDate the end date of the previous occurrence. * @return the new calendar item or null if there are no more calendar items from that recurrent * series. */ public CalendarItemTimerTask next(Date previousStartDate, Date previousEndDate) { if (dateOutOfRange(new Date())) { return null; } Date startDate = previousStartDate; Date endDate = null; boolean executeNow = false; long duration = sourceTask.getEndDate().getTime() - sourceTask.getStartDate().getTime(); switch (patternType) { case Day: { startDate = new Date(startDate.getTime() + period * 60000); endDate = new Date(previousEndDate.getTime() + period * 60000); Date currentDate = new Date(); if (endDate.before(currentDate)) { long offset = currentDate.getTime() - endDate.getTime(); offset -= offset % (period * 60000); if (endDate.getTime() + offset < currentDate.getTime()) { offset += period * 60000; } startDate = new Date(startDate.getTime() + offset); } Calendar cal = Calendar.getInstance(); cal.setTime(startDate); Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); while (deletedInstances.contains(cal.getTime())) { cal.add(Calendar.MINUTE, period); cal2.add(Calendar.MINUTE, period); } if (dateOutOfRange(cal.getTime())) { return null; } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); } case Week: { Calendar cal = Calendar.getInstance(); /** The enum for the firstDow field is the same as Calendar day of week enum + 1 day */ cal.setFirstDayOfWeek(firstDow + 1); cal.setTime(startDate); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); int index = allowedDaysOfWeek.indexOf(dayOfWeek); if (++index < allowedDaysOfWeek.size()) { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); startDate = cal.getTime(); endDate = new Date(startDate.getTime() + duration); } else { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0)); cal.add(Calendar.WEEK_OF_YEAR, period); startDate = cal.getTime(); endDate = new Date(startDate.getTime() + duration); } Date currentDate = new Date(); if (endDate.before(currentDate)) { cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(0)); endDate = new Date(cal.getTimeInMillis() + duration); long offset = (currentDate.getTime() - endDate.getTime()); // 1 week = 604800000 is milliseconds offset -= offset % (period * 604800000); if (endDate.getTime() + offset < currentDate.getTime()) { cal.add(Calendar.WEEK_OF_YEAR, (int) (offset / (period * 604800000))); int i = 1; while (((cal.getTimeInMillis() + duration) < (currentDate.getTime()))) { if (i == allowedDaysOfWeek.size()) { cal.add(Calendar.WEEK_OF_YEAR, period); i = 0; } cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(i)); i++; } startDate = cal.getTime(); } else { startDate = new Date(cal.getTimeInMillis() + offset); } } cal.setTime(startDate); Calendar cal2 = (Calendar) cal.clone(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); index = allowedDaysOfWeek.indexOf(dayOfWeek) + 1; while (deletedInstances.contains(cal.getTime())) { if (index >= allowedDaysOfWeek.size()) { index = 0; cal.add(Calendar.WEEK_OF_YEAR, period); cal2.add(Calendar.WEEK_OF_YEAR, period); } cal.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); cal2.set(Calendar.DAY_OF_WEEK, allowedDaysOfWeek.get(index)); index++; } startDate = cal2.getTime(); endDate = new Date(startDate.getTime() + duration); if (dateOutOfRange(endDate)) return null; if (startDate.before(currentDate)) { executeNow = true; } return new CalendarItemTimerTask( sourceTask.getStatus(), startDate, endDate, sourceTask.getId(), executeNow, this); } case Month: case MonthEnd: case HjMonth: case HjMonthEnd: { return nextMonth(startDate, endDate, false); } case MonthNth: case HjMonthNth: { if (patternSpecific1 == 0x7f && patternSpecific2 == 0x05) { return nextMonth(startDate, endDate, true); } return nextMonthN(startDate, endDate); } } return null; }
public static String getTimeShow(String dateString) { Date thenDate = stringToDate(dateString); Calendar then = Calendar.getInstance(); then.setTime(thenDate); Calendar current = Calendar.getInstance(); long differ = current.getTimeInMillis() - then.getTimeInMillis(); if (differ < 0) { return "Milai"; } else { if (differ >= 0L && differ < 300000L) { return "Just now"; } if (differ >= 300000L && differ < 10800000L) { return MessageFormat.format("{0}:{1}", then.get(Calendar.HOUR), then.get(Calendar.MINUTE)); } if (then.get(Calendar.YEAR) != current.get(Calendar.YEAR)) { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } if (current.get(Calendar.DAY_OF_YEAR) - then.get(Calendar.DAY_OF_YEAR) == 1) { return "Yesterday"; } if (then.get(Calendar.MONTH) != current.get(Calendar.MONTH)) { MDebug.log("[DATE]" + then.get(Calendar.DATE)); return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DAY_OF_MONTH)); } int dayDiff = current.get(Calendar.DAY_OF_MONTH) - then.get(Calendar.DAY_OF_MONTH); if (dayDiff > 7) { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } else { if (dayDiff == 0) { int hour = then.get(Calendar.HOUR); if (0 <= hour && hour < 5) { return "MidNight"; } else if (5 <= hour && hour < 12) { return "BeforeNoon"; } else if (12 <= hour && hour < 19) { return "AfterNoon"; } else if (19 <= hour && hour != 0) { return "Night"; } } if (current.get(Calendar.WEEK_OF_MONTH) == then.get(Calendar.WEEK_OF_MONTH)) { return getDayString(then.get(Calendar.DAY_OF_WEEK)); } else { return MessageFormat.format( "{0}/{1}/{2}", then.get(Calendar.YEAR) - 2000, then.get(Calendar.MONTH), then.get(Calendar.DATE)); } } } }
private int getDay(long miliSecs) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date(miliSecs)); return calendar.get(calendar.DAY_OF_MONTH); }