/** * Set up an event to hang the bets off */ public default void createFutureEvent() { // Grab some horses to use as runners in races final IMap<Horse, Object> fromHC = getClient().getMap("winners"); final Set<Horse> horses = fromHC.keySet(); // Now set up some future-dated events for next Sat final LocalDate nextSat = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SATURDAY)); LocalTime raceTime = LocalTime.of(11, 0); // 1100 start final Event e = CentralFactory.eventOf("Racing from Epsom", nextSat); final Set<Horse> runners = makeRunners(horses, 10); for (int i = 0; i < 18; i++) { final Map<Horse, Double> runnersWithOdds = makeSimulatedOdds(runners); final Race r = CentralFactory.raceOf(LocalDateTime.of(nextSat, raceTime), runnersWithOdds); e.addRace(r); raceTime = raceTime.plusMinutes(10); } final IMap<Long, Event> events = getClient().getMap("events"); events.put(e.getID(), e); }
public ZonedDateTime toIntervalStart(ZonedDateTime time) { ZonedDateTime start; switch (ChronoUnit.valueOf(unit.toString().toUpperCase())) { case SECONDS: start = time.truncatedTo(ChronoUnit.MINUTES); break; case MINUTES: start = time.truncatedTo(ChronoUnit.HOURS); break; case HOURS: start = time.truncatedTo(ChronoUnit.DAYS); break; case DAYS: start = time.with(TemporalAdjusters.firstDayOfYear()).truncatedTo(ChronoUnit.DAYS); break; default: start = time.with(TemporalAdjusters.firstDayOfYear()).truncatedTo(ChronoUnit.DAYS); } long haveMillis = Duration.between(start, time).toMillis(); long maxMillis = duration.toMillis(); long periods = haveMillis / maxMillis; start = start.plus(duration.multipliedBy(periods)); return start; }
public static void PrintfMouth(LocalDate ldb){ LocalDate lde = ldb.with(TemporalAdjusters.lastDayOfMonth()); String mouth = ldb.getMonth().toString(); System.out.println(mouth.substring(0, 1) + mouth.substring(1).toLowerCase() + " " + Integer.toString(ldb.getYear())); System.out.println("Su Mo Tu We Th Fr Sa"); for(int i = 1; i<=lde.getDayOfMonth() + ldb.getDayOfWeek().getValue(); i++){ if(i<=ldb.getDayOfWeek().getValue()){ System.out.print(" "); } else{ System.out.print(String.format("%02d ", i - ldb.getDayOfWeek().getValue())); } if(i%7==0){ System.out.println(); } } }
/** * Returns java.util weekend date (Sunday) based on the current local date. * * @return */ public static Date getCurrentTimesheetWeekEndDate() { LocalDate lastSunday = LocalDate.now() .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("lastSunday: " + lastSunday); return Date.from(lastSunday.atStartOfDay(ZoneId.systemDefault()).toInstant()); }
@Test public void test_next() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(false); i++) { LocalDate date = date(2007, month, i); for (DayOfWeek dow : DayOfWeek.values()) { LocalDate test = (LocalDate) TemporalAdjusters.next(dow).adjustInto(date); assertSame(test.getDayOfWeek(), dow, date + " " + test); if (test.getYear() == 2007) { int dayDiff = test.getDayOfYear() - date.getDayOfYear(); assertTrue(dayDiff > 0 && dayDiff < 8); } else { assertSame(month, Month.DECEMBER); assertTrue(date.getDayOfMonth() > 24); assertEquals(test.getYear(), 2008); assertSame(test.getMonth(), Month.JANUARY); assertTrue(test.getDayOfMonth() < 8); } } } } }
ChronoLocalDate resolveYMD(Map<TemporalField, Long> fieldValues, ResolverStyle resolverStyle) { int y = range(YEAR).checkValidIntValue(fieldValues.remove(YEAR), YEAR); if (resolverStyle == ResolverStyle.LENIENT) { long months = Math.subtractExact(fieldValues.remove(MONTH_OF_YEAR), 1); long days = Math.subtractExact(fieldValues.remove(DAY_OF_MONTH), 1); return date(y, 1, 1).plus(months, MONTHS).plus(days, DAYS); } int moy = range(MONTH_OF_YEAR).checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR), MONTH_OF_YEAR); ValueRange domRange = range(DAY_OF_MONTH); int dom = domRange.checkValidIntValue(fieldValues.remove(DAY_OF_MONTH), DAY_OF_MONTH); if (resolverStyle == ResolverStyle.SMART) { // previous valid try { return date(y, moy, dom); } catch (DateTimeException ex) { return date(y, moy, 1).with(TemporalAdjusters.lastDayOfMonth()); } } return date(y, moy, dom); }
@Override public Instant instantOfNextFrame(final Instant instant) { final ZonedDateTime britishTime = instant.atZone(BRITISH_TIME_ZONE); final DayOfWeek britishDay = britishTime.getDayOfWeek(); if (britishDay != SUNDAY) { return removeMinutesAndLess(britishTime).withHour(0).plus(1, DAYS).toInstant(); } final ZonedDateTime britishTimeCorrectTimeValues = removeMinutesAndLess(britishTime).withHour(22); if (britishTime.getDayOfWeek() == SUNDAY) { if (britishTime.isBefore(britishTimeCorrectTimeValues)) { return britishTimeCorrectTimeValues.toInstant(); } return removeMinutesAndLess(britishTime).withHour(0).plus(2, DAYS).toInstant(); } return britishTimeCorrectTimeValues.with(TemporalAdjusters.next(SUNDAY)).toInstant(); }
@Test public void test_previous() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(false); i++) { LocalDate date = date(2007, month, i); for (DayOfWeek dow : DayOfWeek.values()) { LocalDate test = (LocalDate) TemporalAdjusters.previous(dow).adjustInto(date); assertSame(test.getDayOfWeek(), dow, date + " " + test); if (test.getYear() == 2007) { int dayDiff = test.getDayOfYear() - date.getDayOfYear(); assertTrue(dayDiff < 0 && dayDiff > -8, dayDiff + " " + test); } else { assertSame(month, Month.JANUARY); assertTrue(date.getDayOfMonth() < 8); assertEquals(test.getYear(), 2006); assertSame(test.getMonth(), Month.DECEMBER); assertTrue(test.getDayOfMonth() > 24); } } } } }
@Test(dataProvider = "dayOfWeekInMonth_negative") public void test_lastInMonth(int year, int month, DayOfWeek dow, LocalDate expected) { for (int day = 1; day <= Month.of(month).length(false); day++) { LocalDate date = date(year, month, day); LocalDate test = (LocalDate) TemporalAdjusters.lastInMonth(dow).adjustInto(date); assertEquals(test, expected, "day-of-month=" + day); } }
@Test public void test_lastDayOfYear_nonLeap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(false); i++) { LocalDate date = date(2007, month, i); LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date); assertEquals(test.getYear(), 2007); assertEquals(test.getMonth(), Month.DECEMBER); assertEquals(test.getDayOfMonth(), 31); } } }
public static void main(String[] args) { LocalDate ldb = null; try{ if(args[0].length() == 1){ args[0] = "0" + args[0]; } ldb = LocalDate.parse(args[0] + args[1] + "01",DateTimeFormatter.ofPattern("MMyyyydd")); } catch(Exception e){ ldb = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()); } PrintfMouth(ldb); }
private String formattedPlannedDay(DayOfWeek dayOfDeparture) { LocalDateTime today = LocalDateTime.now(IN_SYDNEY).withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime plannedDay = today.with(TemporalAdjusters.nextOrSame(dayOfDeparture)); if (DAYS.between(today, plannedDay) == 0) { return "Today (" + plannedDay.format(DateTimeFormatter.ofPattern("EEE")) + ")"; } else if (DAYS.between(today, plannedDay) == 1) { return "Tomorrow (" + plannedDay.format(DateTimeFormatter.ofPattern("EEE")) + ")"; } else { return plannedDay.format(DateTimeFormatter.ofPattern("dd MMM (EEE)")); } }
/** * Returns java.time LocalDate weekend date (Sunday) based on the current local date. * * @return */ public static LocalDate getLocalTimesheetWeekEndDate() { log.info("Inside getLocalTimesheetWeekEndDate"); LocalDate lastSunday = LocalDate.now() .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("lastSunday: " + lastSunday); return lastSunday; }
/** * Returns java.time LocalDate weekend date (Sunday) for a given date. * * @return */ public static LocalDate getLocalTimesheetWeekStartDate(Date inputDate) { log.info("Inside getLocalTimesheetWeekStartDate :: inputDate: " + inputDate); if (inputDate != null) { LocalDate localDate = inputDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate firstMonday = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); log.info("Inside inputDate not null loop, firstMonday: " + firstMonday); return firstMonday; } else { log.error("Input date not found. So returning null."); return null; } }
/** * Returns java.util weekend date (Sunday) for a given date. * * @return */ public static Date getTimesheetWeekEndDate(Date inputDate) { log.info("Inside getTimesheetWeekEndDate :: inputDate: " + inputDate); if (inputDate != null) { LocalDate localDate = inputDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate lastSunday = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("Inside input date not null loop, lastSunday: " + lastSunday); return Date.from(lastSunday.atStartOfDay(ZoneId.systemDefault()).toInstant()); } else { log.error("Input date not found. So returning null."); return null; } }
/** * Returns java.time LocalDate weekend date (Sunday) for a given date. * * @return */ public static LocalDate getLocalTimesheetWeekEndDate(Date inputDate) { log.info("Inside getLocalTimesheetWeekEndDate :: inputDate: " + inputDate); if (inputDate != null) { LocalDate localDate = inputDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); LocalDate lastSunday = localDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("Inside inputDate not null loop, lastSunday: " + lastSunday); return lastSunday; } else { log.error("Input date not found. So returning null."); return null; } }
/** * Returns list of LocaDate for end of timesheet week for last three months from current weekend Date. * * @return */ public static List<Date> getListWeekEndDatesOfLastThreeMonths() { log.info("Inside getListEndDatesOfLastThreeMonths method of DateUtils."); LocalDate lastSunday = LocalDate.now() .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("lastSunday: " + lastSunday); long weeks = ChronoUnit.WEEKS.between(lastSunday.minusMonths(3L), lastSunday); log.info("Number of weeks in last three months: " + weeks); List<Date> collect = Stream.iterate(lastSunday.minusWeeks(weeks), d -> d.plusWeeks(1L)) .limit(weeks + 1) .map(d -> Date.from(d.atStartOfDay(ZoneId.systemDefault()).toInstant())) .collect(Collectors.toList()); return collect; }
/** * Returns list of java util end of timesheet week Dates for last three months from current weekend Date. * * @return */ public static List<LocalDate> getListLocalWeekEndDatesOfLastThreeMonths() { log.info("Inside getListLocalEndDatesOfLastThreeMonths method of DateUtils."); LocalDate lastSunday = LocalDate.now() .with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); log.info("lastSunday: " + lastSunday); long weeks = ChronoUnit.WEEKS.between(lastSunday.minusMonths(3L), lastSunday); log.info("Number of weeks in last three months: " + weeks); List<LocalDate> collect = Stream.iterate(lastSunday.minusWeeks(weeks), d -> d.plusWeeks(1L)) .limit(weeks + 1) .collect(Collectors.toList()); return collect; }
/** * Returns list of LocaDate for start of timesheet week Dates for last three months from current week. * * @return */ public static List<Date> getListWeekStartDatesOfLastThreeMonths() { log.info("Inside getListWeekStartDatesOfLastThreeMonths method of DateUtils."); LocalDate startMonday = LocalDate.now() .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); long weeks = ChronoUnit.WEEKS.between(startMonday.minusMonths(3L), startMonday); log.info("Number of weeks in last three months: " + weeks); List<Date> collect = Stream.iterate(startMonday.minusWeeks(weeks), d -> d.plusWeeks(1L)) .limit(weeks + 1) .map(d -> Date.from(d.atStartOfDay(ZoneId.systemDefault()).toInstant())) .collect(Collectors.toList()); return collect; }
@Test public void test_lastDayOfYear_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfYear().adjustInto(date); assertEquals(test.getYear(), 2008); assertEquals(test.getMonth(), Month.DECEMBER); assertEquals(test.getDayOfMonth(), 31); } } }
@Test(dataProvider = "dayOfWeekInMonth_positive") public void test_firstInMonth(int year, int month, DayOfWeek dow, LocalDate expected) { for (int day = 1; day <= Month.of(month).length(false); day++) { LocalDate date = date(year, month, day); LocalDate test = (LocalDate) TemporalAdjusters.firstInMonth(dow).adjustInto(date); assertEquals(test, expected, "day-of-month=" + day); } }
/** * Initializes the class field containing the bookings at the resort for the given month. */ private void getBookingsForMonth() { BookingService bookingService = new BookingService(); // We have to use the old date API here because of JPA specs Date d1 = Date.from(selectedMonth.toInstant(ZoneOffset.UTC)); LocalDateTime lastDayInSelectedMonth = this.selectedMonth.with(TemporalAdjusters.lastDayOfMonth()); Date d2 = Date.from(lastDayInSelectedMonth.toInstant(ZoneOffset.UTC)); this.bookingsForMonth = bookingService.getBookingsBetweenDates(d1, d2); }
@Test public void testDateAPIUtilities() { LocalDate today = LocalDate.now(); System.out.println(today.atStartOfDay()); System.out.println(OffsetTime.now().withHour(new Random().nextInt(24)).withMinute(0).withSecond(0)); //Get the Year, check if it's leap year System.out.println("Year " + today.getYear() + " is Leap Year? " + today.isLeapYear()); //Compare two LocalDate for before and after System.out.println("Today is before 01/01/2015? " + today.isBefore(LocalDate.of(2015, 11, 1))); //Create LocalDateTime from LocalDate System.out.println("Current Time=" + today.atTime(LocalTime.now())); //plus and minus operations System.out.println("10 days after today will be " + today.plusDays(10)); System.out.println("3 weeks after today will be " + today.plusWeeks(3)); System.out.println("20 months after today will be " + today.plusMonths(20)); System.out.println("10 days before today will be " + today.minusDays(10)); System.out.println("3 weeks before today will be " + today.minusWeeks(3)); System.out.println("20 months before today will be " + today.minusMonths(20)); //Temporal adjusters for adjusting the dates System.out.println("First date of this month= " + today.with(TemporalAdjusters.firstDayOfMonth())); LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear()); System.out.println("Last date of this year= " + lastDayOfYear); Period period = today.until(lastDayOfYear); System.out.println("Period Format= " + period); System.out.println("Months remaining in the year= " + period.getMonths()); }
@Test public void test_lastDayOfMonth_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.lastDayOfMonth().adjustInto(date); assertEquals(test.getYear(), 2008); assertEquals(test.getMonth(), month); assertEquals(test.getDayOfMonth(), month.length(true)); } } }
@Test public void test_previousOrCurrent() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(false); i++) { LocalDate date = date(2007, month, i); for (DayOfWeek dow : DayOfWeek.values()) { LocalDate test = (LocalDate) TemporalAdjusters.previousOrSame(dow).adjustInto(date); assertSame(test.getDayOfWeek(), dow); if (test.getYear() == 2007) { int dayDiff = test.getDayOfYear() - date.getDayOfYear(); assertTrue(dayDiff <= 0 && dayDiff > -7); assertEquals(date.equals(test), date.getDayOfWeek() == dow); } else { assertFalse(date.getDayOfWeek() == dow); assertSame(month, Month.JANUARY); assertTrue(date.getDayOfMonth() < 7); assertEquals(test.getYear(), 2006); assertSame(test.getMonth(), Month.DECEMBER); assertTrue(test.getDayOfMonth() > 25); } } } } }
@Test public void test_firstDayOfMonth_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfMonth().adjustInto(date); assertEquals(test.getYear(), 2008); assertEquals(test.getMonth(), month); assertEquals(test.getDayOfMonth(), 1); } } }
@Test public void test_firstDayOfNextMonth_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextMonth().adjustInto(date); assertEquals(test.getYear(), month == DECEMBER ? 2009 : 2008); assertEquals(test.getMonth(), month.plus(1)); assertEquals(test.getDayOfMonth(), 1); } } }
@Test(dataProvider = "dayOfWeekInMonth_negative") public void test_dayOfWeekInMonth_negative(int year, int month, DayOfWeek dow, LocalDate expected) { for (int ordinal = 0; ordinal < 5; ordinal++) { for (int day = 1; day <= Month.of(month).length(false); day++) { LocalDate date = date(year, month, day); LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(-1 - ordinal, dow).adjustInto(date); assertEquals(test, expected.minusWeeks(ordinal)); } } }
@Test public void test_firstDayOfNextYear_nonLeap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(false); i++) { LocalDate date = date(2007, month, i); LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date); assertEquals(test.getYear(), 2008); assertEquals(test.getMonth(), JANUARY); assertEquals(test.getDayOfMonth(), 1); } } }
@Test public void test_firstDayOfYear_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfYear().adjustInto(date); assertEquals(test.getYear(), 2008); assertEquals(test.getMonth(), Month.JANUARY); assertEquals(test.getDayOfMonth(), 1); } } }
@Test public void test_firstDayOfNextYear_leap() { for (Month month : Month.values()) { for (int i = 1; i <= month.length(true); i++) { LocalDate date = date(2008, month, i); LocalDate test = (LocalDate) TemporalAdjusters.firstDayOfNextYear().adjustInto(date); assertEquals(test.getYear(), 2009); assertEquals(test.getMonth(), JANUARY); assertEquals(test.getDayOfMonth(), 1); } } }
@Test(dataProvider = "dayOfWeekInMonth_positive") public void test_dayOfWeekInMonth_positive(int year, int month, DayOfWeek dow, LocalDate expected) { for (int ordinal = 1; ordinal <= 5; ordinal++) { for (int day = 1; day <= Month.of(month).length(false); day++) { LocalDate date = date(year, month, day); LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(ordinal, dow).adjustInto(date); assertEquals(test, expected.plusWeeks(ordinal - 1)); } } }
@Test(dataProvider = "dayOfWeekInMonth_zero") public void test_dayOfWeekInMonth_zero(int year, int month, DayOfWeek dow, LocalDate expected) { for (int day = 1; day <= Month.of(month).length(false); day++) { LocalDate date = date(year, month, day); LocalDate test = (LocalDate) TemporalAdjusters.dayOfWeekInMonth(0, dow).adjustInto(date); assertEquals(test, expected); } }