private static boolean keepCurrent(Calendar asOfDate) { boolean keepCurrent = false; int monthNow = asOfDate.get(Calendar.MONTH); // March, June, September, and December are expiration months boolean isExpirationMonth = ((monthNow + 1) % 3 == 0); if (isExpirationMonth) { Calendar volumeShiftDate = (Calendar) asOfDate.clone(); // Find first Friday volumeShiftDate.set(Calendar.DAY_OF_MONTH, 1); while (volumeShiftDate.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) { volumeShiftDate.add(Calendar.DAY_OF_MONTH, 1); } // Shift to third Friday volumeShiftDate.add(Calendar.WEEK_OF_MONTH, 2); // Finally, find the day before second Friday volumeShiftDate.add(Calendar.DAY_OF_MONTH, -8); if (asOfDate.before(volumeShiftDate)) { keepCurrent = true; } } return keepCurrent; }
public static long getTimeInMillis(int nType, long lDate, int nRoll) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(lDate); if (nType == MONTH) cal.add(Calendar.MONTH, nRoll); // 월 단위 else if (nType == YEAR) cal.add(Calendar.YEAR, nRoll); // 년 단위 else if (nType == HOUR) cal.add(Calendar.HOUR, nRoll); // 시 단위 else cal.add(Calendar.DATE, nRoll); // 일 단위 return cal.getTimeInMillis(); }
/** * Finds the occurrence of the events in the next months * * @param cal the calendar object * @param lastDay if <tt>true</tt> it will return the last day of the month * @param period the number of months to add * @return the calendar object with set date */ private Calendar incrementMonths(Calendar cal, boolean lastDay, int period) { int dayOfMonth = patternSpecific1; cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.MONTH, period); if (lastDay || (cal.getActualMaximum(Calendar.DAY_OF_MONTH) < dayOfMonth)) dayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); return cal; }
public static void main(String[] av) { /** Today's date */ Calendar now = Calendar.getInstance(); /* Do "DateFormat" using "simple" format. */ SimpleDateFormat formatter = new SimpleDateFormat("E yyyy/MM/dd 'at' hh:mm:ss a zzz"); System.out.println("It is now " + formatter.format(now.getTime())); // Add a "# of years" increment to the existing Calendar object now.add(Calendar.YEAR, -2); System.out.println("Two years ago was " + formatter.format(now.getTime())); }
/** * 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(); }
/** * 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); }
/** * 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; }
private void saveExcelPoject(File file) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("timeplan"); // Заголовок в 0 строке Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Филиал"); cell = row.createCell(1); cell.setCellValue("Город"); Calendar cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy"); for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям cell = row.createCell(i + 2); cell.setCellValue(sdf.format(cal.getTime())); cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } // sheet.setColumnWidth(0, 256); // Цвета ячеек CellStyle[] styles = new CellStyle[6]; styles[0] = wb.createCellStyle(); styles[0].setFillForegroundColor(HSSFColor.RED.index); styles[0].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[1] = wb.createCellStyle(); styles[1].setFillForegroundColor(HSSFColor.GREEN.index); styles[1].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[2] = wb.createCellStyle(); styles[2].setFillForegroundColor(HSSFColor.BLUE.index); styles[2].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[3] = wb.createCellStyle(); styles[3].setFillForegroundColor(HSSFColor.ROSE.index); styles[3].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[4] = wb.createCellStyle(); styles[4].setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); styles[4].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[5] = wb.createCellStyle(); styles[5].setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); styles[5].setFillPattern(FillPatternType.SOLID_FOREGROUND); short rowIdx = 0; for (Region region : this.regions) { row = sheet.createRow(++rowIdx); cell = row.createCell(0); cell.setCellValue(region.filial); cell = row.createCell(1); cell.setCellValue(region.name); cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям short color = region.getDateColorIndex(cal.getTime()); if (color >= 0) { cell = row.createCell(i + 2); cell.setCellStyle(styles[color]); } cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } } try (FileOutputStream fileOut = new FileOutputStream(file)) { wb.write(fileOut); } }
@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"); }