Example usage for java.time LocalDate atStartOfDay

List of usage examples for java.time LocalDate atStartOfDay

Introduction

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

Prototype

public LocalDateTime atStartOfDay() 

Source Link

Document

Combines this date with the time of midnight to create a LocalDateTime at the start of this date.

Usage

From source file:org.talend.dataprep.transformation.actions.date.ComputeTimeSinceTest.java

/**
 * Compute time since .//from   w w w  .j a va 2 s.  c  o  m
 *
 * @param date      the date to compute from.
 * @param pattern   the pattern to use.
 * @param unit      the unit for the result.
 * @param sinceWhen the date to calculate since when
 * @return time since now in the wanted unit.
 */
String computeTimeSince(String date, String pattern, ChronoUnit unit, String sinceWhen) {

    DateTimeFormatter format = DateTimeFormatter.ofPattern(pattern);
    Temporal since;
    if (sinceWhen == null) {
        since = LocalDateTime.now();
    } else {
        since = LocalDateTime.parse(sinceWhen, format);
    }

    LocalDateTime start;
    try {
        start = LocalDateTime.parse(date, format);
    } catch (Exception e) {
        start = null;
    }

    if (start == null) {
        LocalDate temp = LocalDate.parse(date, format);
        start = temp.atStartOfDay();
    }

    Temporal result = LocalDateTime.from(start);
    return String.valueOf(unit.between(result, since));
}

From source file:net.tradelib.core.Series.java

private TreeMap<LocalDateTime, Integer> buildDailyIndex() {
    TreeMap<LocalDateTime, Integer> result = new TreeMap<LocalDateTime, Integer>();
    int ii = 0;//  w ww  .j  a v  a 2s. c  o  m
    while (ii < size()) {
        LocalDate ld = index.get(ii).toLocalDate();
        int jj = ii + 1;
        for (; jj < size(); ++jj) {
            if (!index.get(jj).toLocalDate().equals(ld)) {
                break;
            }
        }
        --jj;

        result.put(ld.atStartOfDay(), jj - ii + 1);

        ii = jj + 1;
    }

    return result;
}

From source file:net.tradelib.misc.StrategyText.java

public static void buildOrdersCsv(String dbUrl, String strategy, LocalDate date, String csvPath)
        throws Exception {
    Connection con = DriverManager.getConnection(dbUrl);

    CSVPrinter printer = null;/*from w w  w. ja va  2  s  .  c o m*/
    if (csvPath != null) {
        // Add withHeader for headers
        printer = CSVFormat.DEFAULT.withDelimiter(',').withHeader(CSV_HEADER)
                .print(new BufferedWriter(new FileWriter(csvPath)));
    }

    int rollMethod = 2;

    DatabaseMetaData dmd = con.getMetaData();
    String driverName = dmd.getDriverName();
    String query = "";
    if (driverName.startsWith("MySQL")) {
        query = STRATEGY_ORDER_QUERY_MYSQL;
    } else {
        query = STRATEGY_ORDER_QUERY;
    }

    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setString(1, strategy);
    pstmt.setTimestamp(2, Timestamp.valueOf(date.atStartOfDay()));
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        JsonObject jo = new Gson().fromJson(rs.getString(9), JsonObject.class);
        JsonArray ja = jo.get("orders").getAsJsonArray();

        int ndays = rs.getInt(12);
        String contract = "";
        if (rollMethod == 1) {
            if (ndays > 1) {
                contract = rs.getString(10);
            } else {
                contract = rs.getString(11);
            }
        } else if (rollMethod == 2) {
            contract = rs.getString(15);
        }

        for (int ii = 0; ii < ja.size(); ++ii) {
            JsonObject jorder = ja.get(ii).getAsJsonObject();

            switch (jorder.get("type").getAsString()) {
            case "EXIT_LONG_STOP":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "EXIT_SHORT_STOP":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "ENTER_LONG":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("MKT");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print("");
                printer.println();
                break;

            case "ENTER_SHORT":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("MKT");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print("");
                printer.println();
                break;

            case "ENTER_LONG_STOP":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "ENTER_LONG_STOP_LIMIT":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP LMT");
                // LmtPrice
                printer.print(formatOrderPrice(jorder.get("limit_price").getAsBigDecimal()));
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "ENTER_SHORT_STOP":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "ENTER_SHORT_STOP_LIMIT":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP LMT");
                // LmtPrice
                printer.print(formatOrderPrice(jorder.get("limit_price").getAsBigDecimal()));
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "EXIT_LONG":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("MKT");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print("");
                printer.println();
                break;

            case "EXIT_SHORT":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("MKT");
                // LmtPrice
                printer.print("");
                // AuxPrice
                printer.print("");
                printer.println();
                break;

            case "EXIT_SHORT_STOP_LIMIT":
                // Action
                printer.print("BUY");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP LMT");
                // LmtPrice
                printer.print(formatOrderPrice(jorder.get("limit_price").getAsBigDecimal()));
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;

            case "EXIT_LONG_STOP_LIMIT":
                // Action
                printer.print("SELL");
                // Quantity
                printer.print(jorder.get("quantity").getAsLong());
                // Symbol
                printer.print(rs.getString(4));
                // SecType
                printer.print(rs.getString(14));
                // LastTradingDayOrContractMonth
                printer.print(contract);
                // Exchange
                printer.print(rs.getString(13));
                // OrderType
                printer.print("STP LMT");
                // LmtPrice
                printer.print(formatOrderPrice(jorder.get("limit_price").getAsBigDecimal()));
                // AuxPrice
                printer.print(formatOrderPrice(jorder.get("stop_price").getAsBigDecimal()));
                printer.println();
                break;
            }
        }

        if (printer != null)
            printer.flush();
    }
}

From source file:net.tradelib.misc.StrategyText.java

public static List<InstrumentText> buildList(Connection con, String strategy, LocalDate date, String csvPath,
        char csvSep) throws Exception {
    // public static List<InstrumentText> buildList(Connection con, String strategy, LocalDate date) throws Exception {
    ArrayList<InstrumentText> result = new ArrayList<InstrumentText>();

    CSVPrinter printer = null;//  w  w w.  j a va  2  s  .com
    if (csvPath != null) {
        // Add withHeader for headers
        printer = CSVFormat.DEFAULT.withDelimiter(csvSep).print(new BufferedWriter(new FileWriter(csvPath)));
    }

    int numCsvColumns = 12;

    int rollMethod = 2;

    DatabaseMetaData dmd = con.getMetaData();
    String driverName = dmd.getDriverName();
    String query = "";
    if (driverName.startsWith("MySQL")) {
        query = STRATEGY_QUERY_MYSQL;
    } else {
        query = STRATEGY_QUERY;
    }

    String prevCategory = "";
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setString(1, strategy);
    pstmt.setTimestamp(2, Timestamp.valueOf(date.atStartOfDay()));
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        String category = rs.getString(2);
        if (!category.equals(prevCategory)) {
            result.add(InstrumentText.makeSection(category));
            prevCategory = category;

            if (printer != null) {
                printer.print(category);
                for (int ii = 1; ii < numCsvColumns; ++ii) {
                    printer.print("");
                }
                printer.println();
            }
        }
        String name = rs.getString(3);
        String symbol = rs.getString(4);
        String contract = "";
        if (rollMethod == 1) {
            // Uses current_contract and trading_days
            int ndays = rs.getInt(12);
            if (ndays > 1) {
                contract = rs.getString(10);
            } else {
                contract = "Roll to " + rs.getString(11);
            }
        } else if (rollMethod == 2) {
            // Uses current_contract2 and roll_today
            int rollToday = rs.getInt(14);
            if (rollToday == 0) {
                contract = rs.getString(13);
            } else {
                contract = "Roll to " + rs.getString(13);
            }
        }

        if (printer != null) {
            printer.print(name);
            printer.print(symbol);
            printer.print(contract);
        }

        String signal;
        long position = (long) rs.getDouble(5);
        JsonObject jo = new Gson().fromJson(rs.getString(9), JsonObject.class);
        if (position > 0.0) {
            BigDecimal entryPrice;
            double pnl;
            try {
                entryPrice = jo.get("entry_price").getAsBigDecimal();
                pnl = jo.get("pnl").getAsDouble();
            } catch (Exception e) {
                entryPrice = BigDecimal.valueOf(Double.MIN_VALUE);
                pnl = Double.MIN_VALUE;
            }
            signal = String.format("Long [%d] since %s [at %s].", position, rs.getString(6),
                    formatBigDecimal(entryPrice));
            if (printer != null)
                printer.print(signal);
            String openProfit = String.format("Open equity profit %,d.", (int) Math.floor(pnl));
            signal += " " + openProfit;
            if (printer != null)
                printer.print(openProfit);
        } else if (position < 0.0) {
            BigDecimal entryPrice;
            double pnl;
            try {
                entryPrice = jo.get("entry_price").getAsBigDecimal();
                pnl = jo.get("pnl").getAsDouble();
            } catch (Exception e) {
                entryPrice = BigDecimal.valueOf(-1);
                pnl = -1;
            }
            signal = String.format("Short [%d] since %s [at %s].", Math.abs(position), rs.getString(6),
                    formatBigDecimal(entryPrice));
            if (printer != null)
                printer.print(signal);
            String openProfit = String.format("Open equity profit %,d.", (int) Math.floor(pnl));
            signal += " " + openProfit;
            if (printer != null)
                printer.print(openProfit);
        } else {
            signal = "Out.";
            if (printer != null) {
                printer.print(signal);
                // An empty column follows the status if there is no position - there is no profit.
                printer.print("");
            }
        }

        boolean hasOrder = false;
        JsonArray ja = jo.get("orders").getAsJsonArray();
        double entryRisk;
        try {
            entryRisk = jo.get("entry_risk").getAsDouble();
        } catch (Exception ee) {
            entryRisk = Double.NaN;
        }
        String profitTarget;
        Double profitTargetDbl;
        try {
            profitTarget = formatBigDecimal(jo.get("profit_target").getAsBigDecimal());
            profitTargetDbl = jo.get("profit_target").getAsDouble();
        } catch (Exception ee) {
            profitTarget = null;
            profitTargetDbl = null;
        }
        String stopLoss;
        Double stopLossDbl;
        try {
            stopLoss = formatBigDecimal(jo.get("stop_loss").getAsBigDecimal());
            stopLossDbl = jo.get("stop_loss").getAsDouble();
        } catch (Exception ee) {
            stopLoss = null;
            stopLossDbl = null;
        }

        Double lastClose;
        try {
            lastClose = jo.get("last_close").getAsDouble();
        } catch (Exception ee) {
            lastClose = null;
        }

        // Currently maximum one entry and maximum one exit are supported.
        String entryStr = "";
        String exitStr = "";
        String contractRiskStr = "";

        for (int ii = 0; ii < ja.size(); ++ii) {
            JsonObject jorder = ja.get(ii).getAsJsonObject();

            switch (jorder.get("type").getAsString()) {
            case "EXIT_LONG_STOP":
                exitStr = "Exit long at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal())
                        + ".";
                signal += " " + exitStr;
                break;

            case "EXIT_SHORT_STOP":
                exitStr = "Exit short at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal())
                        + ".";
                signal += " " + exitStr;
                break;

            case "ENTER_LONG":
                if (!Double.isNaN(entryRisk)) {
                    entryStr = String.format("Enter long at open. Contract risk is %s.",
                            formatDouble(entryRisk, 0, 0));
                    signal += " " + entryStr;
                } else {
                    entryStr = "Enter long at open.";
                    signal += " " + entryStr;
                }
                break;

            case "ENTER_SHORT":
                if (!Double.isNaN(entryRisk)) {
                    entryStr = String.format("Enter short at open. Contract risk is %s.",
                            formatDouble(entryRisk, 0, 0));
                    signal += " " + entryStr;
                } else {
                    entryStr = "Enter short at open.";
                    signal += " " + entryStr;
                }
                break;

            case "ENTER_LONG_STOP":
                position = jorder.get("quantity").getAsLong();
                entryStr = String.format("Enter long [%d] at stop %s [%s%%].", position,
                        formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()),
                        formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
                signal += " " + entryStr;
                if (!Double.isNaN(entryRisk)) {
                    contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
                    signal += " " + contractRiskStr;
                }
                break;

            case "ENTER_LONG_STOP_LIMIT":
                position = jorder.get("quantity").getAsLong();
                entryStr = String.format("Enter long [%d] at limit %s, stop at %s [%s%%].", position,
                        formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()),
                        formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()),
                        formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
                signal += " " + entryStr;
                if (!Double.isNaN(entryRisk)) {
                    contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
                    signal += contractRiskStr;
                }
                break;

            case "ENTER_SHORT_STOP":
                // signal += " Enter short at stop " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + ".";
                position = jorder.get("quantity").getAsLong();
                entryStr = String.format("Enter short [%d] at stop %s [%s%%].", Math.abs(position),
                        formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()),
                        formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
                signal += " " + entryStr;
                if (!Double.isNaN(entryRisk)) {
                    contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
                    signal += " " + contractRiskStr;
                }
                break;

            case "ENTER_SHORT_STOP_LIMIT":
                position = jorder.get("quantity").getAsLong();
                entryStr = String.format("Enter short [%d] at limit %s, stop at %s [%s%%].", Math.abs(position),
                        formatBigDecimal(jorder.get("limit_price").getAsBigDecimal()),
                        formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()),
                        formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100));
                signal += " " + entryStr;
                if (!Double.isNaN(entryRisk)) {
                    contractRiskStr = String.format(" Contract risk is %s.", formatDouble(entryRisk, 0, 0));
                    signal += " " + contractRiskStr;
                }
                break;

            case "EXIT_LONG":
                exitStr = "Exit long at open.";
                signal += " " + exitStr;
                break;

            case "EXIT_SHORT":
                exitStr = "Exit short at open.";
                signal += " " + exitStr;
                break;

            case "EXIT_SHORT_STOP_LIMIT":
                exitStr = "Exit short at limit " + formatBigDecimal(jorder.get("limit_price").getAsBigDecimal())
                        + ", stop at " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + " ["
                        + formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100)
                        + "%]" + ".";
                signal += " " + exitStr;
                break;

            case "EXIT_LONG_STOP_LIMIT":
                exitStr = "Exit long at limit " + formatBigDecimal(jorder.get("limit_price").getAsBigDecimal())
                        + ", stop at " + formatBigDecimal(jorder.get("stop_price").getAsBigDecimal()) + " ["
                        + formatPercentage(jorder.get("stop_price").getAsDouble() / lastClose * 100 - 100)
                        + "%]" + ".";
                signal += " " + exitStr;
                break;
            }
            hasOrder = true;
        }

        String lastCloseStr = "Last close at " + formatBigDecimal(jo.get("last_close").getAsBigDecimal()) + ".";
        String stopLossStr = "";
        String profitTargetStr = "";

        if (hasOrder) {
            signal += " " + lastCloseStr;
        }

        if (stopLoss != null) {
            stopLossStr = "Stop loss at " + stopLoss;
            if (lastClose != null && stopLossDbl != null) {
                stopLossStr += " [" + formatPercentage(stopLossDbl / lastClose * 100 - 100) + "%]";
            }
            stopLossStr += ".";
            signal += " " + stopLossStr;
        }

        if (profitTarget != null) {
            profitTargetStr = "Profit target at about " + profitTarget;
            if (profitTargetDbl != null && lastClose != null) {
                profitTargetStr += " [" + formatPercentage(profitTargetDbl / lastClose * 100 - 100) + "%]";
            }
            profitTargetStr += ".";
            signal += " " + profitTargetStr;
        }

        if (printer != null) {
            printer.print(exitStr);
            printer.print(entryStr);
            printer.print(contractRiskStr);
            printer.print(lastCloseStr);
            printer.print(stopLossStr);
            printer.print(profitTargetStr);
            printer.println();
        }

        result.add(InstrumentText.make(name, symbol, contract, signal));
    }

    rs.close();
    pstmt.close();

    if (printer != null)
        printer.flush();

    return result;
}

From source file:nc.noumea.mairie.appock.services.impl.MailServiceImpl.java

@Override
public void sendMailEcheanceCommandeParReferentAchat(LocalDate datePrevisionnelleCommande)
        throws MessagingException, UnsupportedEncodingException {
    AppockMail appockMail = new AppockMail();
    appockMail.addDestinataire(appUserService.findAllWithRole(Role.ROLE_REFERENT_SERVICE, true));
    appockMail.setSujet("[APPOCK] Rappel d'chance de commande");
    String contenu = "Bonjour,<br/><br/>Une commande est prvue le <b>"
            + (datePrevisionnelleCommande == null ? "?"
                    : DateUtil.formatDate(datePrevisionnelleCommande.atStartOfDay()))
            + "</b> par les rfrents achat. Veuillez envoyer vos demandes d'approvisionnement au plus tard  cette date.";
    appockMail.setContenu(contenu);/*  w w w  . j av  a 2  s. com*/
    sendMail(appockMail);
}

From source file:net.nikr.eve.jeveasset.gui.tabs.tracker.TrackerTab.java

private Date getFromDate() {
    LocalDate date = jFrom.getDate();
    if (date == null) {
        return null;
    }/*from   w  w w.j a  v a2  s.c  om*/
    Instant instant = date.atStartOfDay().atZone(ZoneId.of("GMT")).toInstant(); //Start of day - GMT
    return Date.from(instant);
}