List of usage examples for java.time.temporal ChronoField MONTH_OF_YEAR
ChronoField MONTH_OF_YEAR
To view the source code for java.time.temporal ChronoField MONTH_OF_YEAR.
Click Source Link
From source file:Main.java
/** * Returns the current quarter of the given date * /*from w w w.java 2s . c om*/ * @return int (0 .. 3) * @param cal * Given date, cannot be null */ public static int getQuarter(LocalDate cal) { int month = cal.get(ChronoField.MONTH_OF_YEAR); switch (Month.of(month)) { case JANUARY: case FEBRUARY: case MARCH: default: return 0; case APRIL: case MAY: case JUNE: return 1; case JULY: case AUGUST: case SEPTEMBER: return 2; case OCTOBER: case NOVEMBER: case DECEMBER: return 3; } }
From source file:Main.java
public static Boolean isFamilyBirthday(TemporalAccessor date) { int month = date.get(ChronoField.MONTH_OF_YEAR); int day = date.get(ChronoField.DAY_OF_MONTH); // Angie's birthday is on April 3. if ((month == Month.APRIL.getValue()) && (day == 3)) return Boolean.TRUE; // Sue's birthday is on June 18. if ((month == Month.JUNE.getValue()) && (day == 18)) return Boolean.TRUE; // Joe's birthday is on May 29. if ((month == Month.MAY.getValue()) && (day == 29)) return Boolean.TRUE; return Boolean.FALSE; }
From source file:Main.java
@Override public Boolean queryFrom(TemporalAccessor date) { int month = date.get(ChronoField.MONTH_OF_YEAR); if (month == Month.JULY.getValue() || month == Month.AUGUST.getValue()) { return true; }/*ww w .j av a 2 s .c o m*/ return false; }
From source file:Main.java
public Boolean queryFrom(TemporalAccessor date) { int month = date.get(ChronoField.MONTH_OF_YEAR); int day = date.get(ChronoField.DAY_OF_MONTH); // Disneyland over Spring Break if ((month == Month.APRIL.getValue()) && ((day >= 3) && (day <= 8))) return Boolean.TRUE; // Smith family reunion on Lake Saugatuck if ((month == Month.AUGUST.getValue()) && ((day >= 8) && (day <= 14))) return Boolean.TRUE; return Boolean.FALSE; }
From source file:msi.gama.util.GamaDate.java
private static Temporal parse(final IScope scope, final String original, final DateTimeFormatter df) { if (original == null || original.isEmpty() || original.equals("now")) { return LocalDateTime.now(GamaDateType.DEFAULT_ZONE); }/*from www. j a v a 2 s.co m*/ Temporal result = null; if (df != null) { try { final TemporalAccessor ta = df.parse(original); if (ta instanceof Temporal) { return (Temporal) ta; } if (!ta.isSupported(ChronoField.YEAR) && !ta.isSupported(ChronoField.MONTH_OF_YEAR) && !ta.isSupported(ChronoField.DAY_OF_MONTH)) { if (ta.isSupported(ChronoField.HOUR_OF_DAY)) { return LocalTime.from(ta); } } if (!ta.isSupported(ChronoField.HOUR_OF_DAY) && !ta.isSupported(ChronoField.MINUTE_OF_HOUR) && !ta.isSupported(ChronoField.SECOND_OF_MINUTE)) { return LocalDate.from(ta); } return LocalDateTime.from(ta); } catch (final DateTimeParseException e) { e.printStackTrace(); } GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning( "The date " + original + " can not correctly be parsed by the pattern provided", scope), false); return parse(scope, original, null); } String dateStr; try { // We first make sure all date fields have the correct length and // the string is correctly formatted String string = original; if (!original.contains("T") && original.contains(" ")) { string = StringUtils.replaceOnce(original, " ", "T"); } final String[] base = string.split("T"); final String[] date = base[0].split("-"); String other; if (base.length == 1) { other = "00:00:00"; } else { other = base[1]; } String year, month, day; if (date.length == 1) { // ISO basic date format year = date[0].substring(0, 4); month = date[0].substring(4, 6); day = date[0].substring(6, 8); } else { year = date[0]; month = date[1]; day = date[2]; } if (year.length() == 2) { year = "20" + year; } if (month.length() == 1) { month = '0' + month; } if (day.length() == 1) { day = '0' + day; } dateStr = year + "-" + month + "-" + day + "T" + other; } catch (final Exception e1) { throw GamaRuntimeException.error("The date " + original + " is not correctly formatted. Please refer to the ISO date/time format", scope); } try { result = LocalDateTime.parse(dateStr); } catch (final DateTimeParseException e) { try { result = OffsetDateTime.parse(dateStr); } catch (final DateTimeParseException e2) { try { result = ZonedDateTime.parse(dateStr); } catch (final DateTimeParseException e3) { throw GamaRuntimeException.error( "The date " + original + " is not correctly formatted. Please refer to the ISO date/time format", scope); } } } return result; }
From source file:com.github.aptd.simulation.datamodel.CXMLReader.java
@Override @SuppressWarnings("unchecked") public final IExperiment get(final IFactory p_factory, final String p_datamodel, final long p_simulationsteps, final boolean p_parallel, final String p_timemodel, final Supplier<RealDistribution> p_platformchangedurationdistributionsupplier, final int p_numberofpassengers, final double p_lightbarrierminfreetime, final double p_delayseconds) { try (final FileInputStream l_stream = new FileInputStream(p_datamodel)) { final Asimov l_model = (Asimov) m_context.createUnmarshaller().unmarshal(l_stream); // time definition final Instant l_starttime = ZonedDateTime.now(ZoneId.systemDefault()) .with(ChronoField.CLOCK_HOUR_OF_DAY, 8).with(ChronoField.MINUTE_OF_HOUR, 45) .with(ChronoField.SECOND_OF_MINUTE, 0).with(ChronoField.NANO_OF_SECOND, 0) .with(ChronoField.DAY_OF_MONTH, 3).with(ChronoField.MONTH_OF_YEAR, 10) .with(ChronoField.YEAR, 2017).toInstant(); final ITime l_time = "jump".equals(p_timemodel) ? new CJumpTime(l_starttime, p_simulationsteps) : new CStepTime(l_starttime, Duration.ofSeconds(1), p_simulationsteps); final CMessenger l_messenger = new CMessenger(); final Set<IAction> l_actionsfrompackage = CCommon.actionsFromPackage().collect(Collectors.toSet()); // asl agent definition final Map<String, String> l_agentdefs = agents(l_model.getAi()); // macro (train-network) and microscopic model final Map<String, IPlatform<?>> l_platform = platform(l_model.getNetwork(), l_agentdefs, p_factory, l_time);// w ww . j av a2 s . c om final Map<String, IStation<?>> l_station = station(l_model.getNetwork(), l_agentdefs, p_factory, l_time, l_platform); final Pair<Map<String, ITrain<?>>, Map<String, IDoor<?>>> l_train = train(l_model.getNetwork(), l_agentdefs, p_factory, l_time, p_lightbarrierminfreetime); final Map<String, IElement<?>> l_agents = new HashMap<>(); l_agents.putAll(l_platform); l_agents.putAll(l_station); l_agents.putAll(l_train.getLeft()); l_agents.putAll(l_train.getRight()); final CExperiment l_experiment = new CExperiment(p_simulationsteps, p_parallel, IStatistic.EMPTY, l_agents, l_time, l_messenger); // @todo create passengersources and their passenger generators according to scenario definition final IElement.IGenerator<IPassenger<?>> l_passengergenerator = passengergenerator(p_factory, "+!activate <-\n state/transition\n.", l_actionsfrompackage, l_time); l_experiment.addAgent("passengersource_test", passengersourcegenerator(p_factory, "+!activate <-\n state/transition\n.", l_actionsfrompackage, l_time).generatesingle(new UniformRealDistribution(0.0, 1.0), l_time.current().toEpochMilli(), p_numberofpassengers, l_passengergenerator, l_experiment, l_agents.get("toy-node-1"), p_platformchangedurationdistributionsupplier.get())); l_messenger.experiment(l_experiment); // experiment (executable model) return l_experiment; } catch (final Exception l_execption) { throw new CRuntimeException(l_execption); } }
From source file:ch.algotrader.service.tt.TTFixReferenceDataServiceImpl.java
private void retrieveFutures(final FutureFamily securityFamily, final List<TTSecurityDefVO> securityDefs) { // get all current futures List<Future> allFutures = this.futureDao.findBySecurityFamily(securityFamily.getId()); Map<String, Future> mapByTtid = allFutures.stream().filter(e -> e.getTtid() != null) .collect(Collectors.toMap(e -> e.getTtid(), e -> e)); Map<String, Future> mapBySymbol = allFutures.stream().collect(Collectors.toMap(e -> e.getSymbol(), e -> e)); for (TTSecurityDefVO securityDef : securityDefs) { String type = securityDef.getType(); if (!type.equalsIgnoreCase("FUT")) { throw new ServiceException("Unexpected security definition type for future: " + type); }/*from w w w . j av a 2 s. co m*/ String id = securityDef.getId(); if (!mapByTtid.containsKey(id)) { LocalDate maturityDate = securityDef.getMaturityDate(); LocalDate expiration = maturityDate.withDayOfMonth(1); // IPE e-Brent has to be handled as a special case as it happens to have multiple contracts // with the same expiration month String symbol; if ("ICE_IPE".equalsIgnoreCase(securityDef.getExchange()) && securityDef.getAltSymbol() != null) { String altSymbol = securityDef.getAltSymbol(); if (altSymbol.startsWith("Q") || altSymbol.startsWith("Cal")) { continue; } else { try { TemporalAccessor parsed = ICE_IPE_SYMBOL.parse(altSymbol); int year = parsed.get(ChronoField.YEAR); int month = parsed.get(ChronoField.MONTH_OF_YEAR); expiration = LocalDate.of(year, month, 1); symbol = FutureSymbol.getSymbol(securityFamily, expiration, this.commonConfig.getFutureSymbolPattern()); } catch (DateTimeParseException ex) { throw new ServiceException( "Unable to parse IPE e-Brent expiration month / year: " + altSymbol, ex); } } } else { symbol = FutureSymbol.getSymbol(securityFamily, maturityDate, this.commonConfig.getFutureSymbolPattern()); } if (!mapBySymbol.containsKey(symbol)) { Future future = Future.Factory.newInstance(); String isin = securityFamily.getIsinRoot() != null ? FutureSymbol.getIsin(securityFamily, maturityDate) : null; String ric = securityFamily.getRicRoot() != null ? FutureSymbol.getRic(securityFamily, maturityDate) : null; future.setSymbol(symbol); future.setIsin(isin); future.setRic(ric); future.setTtid(id); future.setExpiration(DateTimeLegacy.toLocalDate(expiration)); future.setMonthYear(DateTimePatterns.MONTH_YEAR.format(maturityDate)); future.setSecurityFamily(securityFamily); future.setUnderlying(securityFamily.getUnderlying()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Creating future based on TT definition: {} {}", securityDef.getSymbol(), securityDef.getMaturityDate()); } this.futureDao.save(future); } else { Future future = mapBySymbol.get(symbol); future.setTtid(id); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Updating future based on TT definition: {} {}", securityDef.getSymbol(), securityDef.getMaturityDate()); } } } } }
From source file:ch.algotrader.service.ib.IBNativeReferenceDataServiceImpl.java
private LocalDate parseYearMonth(final CharSequence s) { if (s == null || s.length() == 0) { return null; }//from w ww .ja v a2s. c om try { TemporalAccessor parsed = YEAR_MONTH_FORMAT.parse(s); int year = parsed.get(ChronoField.YEAR); int month = parsed.get(ChronoField.MONTH_OF_YEAR); return LocalDate.of(year, month, 1); } catch (DateTimeParseException ex) { throw new ServiceException("Invalid year/month format: " + s); } }
From source file:org.silverpeas.core.calendar.CalendarEventOccurrenceGenerationTest.java
@Test public void countEventOccurrencesInYear() { List<CalendarEventOccurrence> occurrences = generator.generateOccurrencesOf(calendarEventsForTest(), in(Year.of(2016)));/*from w w w . j ava 2s . co m*/ assertThat(occurrences.isEmpty(), is(false)); assertThat(occurrences.size(), is(103)); // compute the occurrence count both per month and per event int[] occurrenceCountPerMonth = new int[12]; int[] occurrenceCountPerEvent = new int[6]; occurrences.forEach(o -> { occurrenceCountPerMonth[o.getStartDate().get(ChronoField.MONTH_OF_YEAR) - 1] += 1; occurrenceCountPerEvent[Integer.parseInt(o.getCalendarEvent().getAttributes().get(ATTR_TEST_ID).get()) - 1] += 1; }); // check now the count of occurrences per month is ok assertThat(occurrenceCountPerMonth[JANUARY.ordinal()], is(0)); assertThat(occurrenceCountPerMonth[FEBRUARY.ordinal()], is(0)); assertThat(occurrenceCountPerMonth[MARCH.ordinal()], is(4)); assertThat(occurrenceCountPerMonth[APRIL.ordinal()], is(6)); assertThat(occurrenceCountPerMonth[MAY.ordinal()], is(10)); assertThat(occurrenceCountPerMonth[JUNE.ordinal()], is(10)); assertThat(occurrenceCountPerMonth[JULY.ordinal()], is(4)); assertThat(occurrenceCountPerMonth[AUGUST.ordinal()], is(5)); assertThat(occurrenceCountPerMonth[SEPTEMBER.ordinal()], is(18)); assertThat(occurrenceCountPerMonth[OCTOBER.ordinal()], is(17)); assertThat(occurrenceCountPerMonth[NOVEMBER.ordinal()], is(17)); assertThat(occurrenceCountPerMonth[DECEMBER.ordinal()], is(12)); // check now the count of occurrences per event is ok assertThat(occurrenceCountPerEvent[Integer.parseInt("1") - 1], is(1)); assertThat(occurrenceCountPerEvent[Integer.parseInt("2") - 1], is(1)); assertThat(occurrenceCountPerEvent[Integer.parseInt("3") - 1], is(42)); assertThat(occurrenceCountPerEvent[Integer.parseInt("4") - 1], is(1)); assertThat(occurrenceCountPerEvent[Integer.parseInt("5") - 1], is(46)); assertThat(occurrenceCountPerEvent[Integer.parseInt("6") - 1], is(12)); }