Example usage for java.time LocalDate withDayOfMonth

List of usage examples for java.time LocalDate withDayOfMonth

Introduction

In this page you can find the example usage for java.time LocalDate withDayOfMonth.

Prototype

public LocalDate withDayOfMonth(int dayOfMonth) 

Source Link

Document

Returns a copy of this LocalDate with the day-of-month altered.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate a = LocalDate.of(2014, 6, 30);
    LocalDate b = a.withDayOfMonth(10);
    System.out.println(b);//from  w  w  w  .  j  a v a2s  .  c  o m
}

From source file:Main.java

/**
 * The adjustInto method accepts a Temporal instance
 * and returns an adjusted LocalDate. If the passed in
 * parameter is not a LocalDate, then a DateTimeException is thrown.
 *//*from w ww  .java2 s.c o m*/
public Temporal adjustInto(Temporal input) {
    LocalDate date = LocalDate.from(input);
    int day;
    if (date.getDayOfMonth() < 15) {
        day = 15;
    } else {
        day = date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
    }
    date = date.withDayOfMonth(day);
    if (date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
        date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
    }

    return input.with(date);
}

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);
        }//w w w  . j a va  2 s  .c  om
        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:com.jscriptive.moneyfx.ui.chart.ChartFrame.java

private void handleMonthlyInOutChartMouseClickEvent(Account account, LocalDate month, MouseEvent event) {
    if (event.getClickCount() == 2) {
        TransactionFilter filter = new TransactionFilter(account, null, null,
                new ValueRange<>(month.withDayOfMonth(1), month.plusMonths(1).withDayOfMonth(1).minusDays(1)),
                new ValueRange<>(null, null), new ValueRange<>(null, null));
        chartFrame.getScene().lookup("#tabPane").fireEvent(new ShowTransactionsEvent(filter));
    }//from w  w  w .  ja  v  a  2 s.c  om
}