Ejemplo n.º 1
0
  private Month create(final LocalDate year, final LocalDate localMonth) {

    log.debug("Entering with {}, {}", year, localMonth);

    Month month = new Month();

    month.setMonth(localMonth.getMonth());

    for (int i = 0; i < localMonth.getMonth().length(year.isLeapYear()); i++)
      month.getDays().add(create(localMonth.plusDays(i)));

    createWeeks(month);

    return month;
  }
 // See https://en.wikipedia.org/wiki/Day_count_convention#30E.2F360_ISDA
 private int diff360EIsda(final LocalDate start, final LocalDate end) {
   if (start.equals(end)) {
     return 0;
   }
   int dayStart = start.getDayOfMonth();
   int dayEnd = end.getDayOfMonth();
   if (start.getMonth().length(start.isLeapYear()) == dayStart) {
     dayStart = MONTH_30_DAYS;
   }
   if (end.getMonth() != Month.FEBRUARY && end.getMonth().length(end.isLeapYear()) == dayEnd) {
     dayEnd = MONTH_30_DAYS;
   }
   return (end.getYear() - start.getYear()) * YEAR_360
       + (end.getMonthValue() - start.getMonthValue()) * MONTH_30_DAYS
       + dayEnd
       - dayStart;
 }
  public static void main(String[] args) {
    LocalDate today = LocalDate.now(); // Today’s date
    System.out.println("TODAY: " + today);

    /*String strDate = today.plusWeeks(1).toString(); // 2015-09-01
    String day;
    if (strDate.substring(8, 9).matches("0")) {
        day = strDate.substring(9);
        System.out.println("DAY IS: " + day);
    } else {
        day = strDate.substring(8);
        System.out.println("DAY IS: " + day);
    }*/

    int dayOfMonth = today.plusDays(4).getDayOfMonth();
    System.out.println("DAY OF MONTH: " + dayOfMonth);

    LocalDate localDate = LocalDate.of(2015, Month.valueOf("OCTOBER"), 25);
    System.out.println("LOCAL DATE: " + localDate);

    long toDays = Duration.between(localDate.atTime(0, 0), today.atTime(0, 0)).toDays();
    System.out.println("TO DAYS: " + toDays);
    Duration toDays23 = Duration.between(localDate, today);
    System.out.println("TO DAYS: " + toDays23);

    Month month = today.getMonth();
    //        localDate.
    System.out.println("Month " + month.toString());
    String displayName = month.getDisplayName(TextStyle.FULL, Locale.CANADA);
    System.out.println("DISPLAY NAME: " + displayName);

    LocalDate alonzosBirthday = LocalDate.of(1903, 6, 14);
    alonzosBirthday = LocalDate.of(1903, Month.JUNE, 14);
    // Uses the Month enumeration
    System.out.println("alonzosBirthday: " + alonzosBirthday);

    LocalDate programmersDay = LocalDate.of(2015, 1, 1).plusDays(255);
    // September 13, but in a leap year it would be September 12
    System.out.println("programmersDay: " + programmersDay);

    LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
    LocalDate christmas = LocalDate.of(2014, Month.DECEMBER, 25);

    System.out.println("Until christmas: " + independenceDay.until(christmas));
    System.out.println(
        "Until christmas (with crono): " + independenceDay.until(christmas, ChronoUnit.DAYS));

    System.out.println(LocalDate.of(2016, 1, 31).plusMonths(1));
    System.out.println(LocalDate.of(2016, 3, 31).minusMonths(1));

    DayOfWeek startOfLastMillennium = LocalDate.of(1900, 1, 1).getDayOfWeek();
    System.out.println("startOfLastMillennium: " + startOfLastMillennium);
    System.out.println(startOfLastMillennium.getValue());
    System.out.println(DayOfWeek.SATURDAY.plus(3));
  }
Ejemplo n.º 4
0
  /**
   * Prende i valori inseriti e ci crea un istanza.
   *
   * @return
   */
  public Cliente prendiDatiDaView() throws CommonException {
    LocalDate dParam = null;
    cliente = new Cliente();
    if (txtNome.getText().isEmpty()
        || txtCognome.getText().isEmpty()
        || dNascita.getValue() == null
        || dEmissPatente.getValue() == null
        || txtIndirizzo.getText().isEmpty()
        || txtCodFisc.getText().isEmpty()
        || txtNumCel.getText().isEmpty()
        || txtPatGuida.getText().isEmpty()
        || txtEmail.getText().isEmpty()) {

      throw new CommonException(
          "Prima di procedere è necessario compilare tutti i campi obbligatori");
    } else {

      cliente.setNome(txtNome.getText());

      cliente.setCognome(txtCognome.getText());

      cliente.setSesso(((RadioButton) group.getSelectedToggle()).getText());
      System.out.println(((RadioButton) group.getSelectedToggle()).getText());
      cliente.setDatanascita(dNascita.getValue());

      cliente.setDataEmissPatente(dEmissPatente.getValue());

      // data scadenza patente -> dataEmissione + 10 anni.
      dParam = dEmissPatente.getValue();
      dScadPatente.setValue(
          LocalDate.of(dParam.getYear() + 10, dParam.getMonth(), dParam.getDayOfMonth()));
      cliente.setDataScadPatente(dScadPatente.getValue());

      cliente.setIndirizzo(txtIndirizzo.getText());

      cliente.setCodFiscale(txtCodFisc.getText());

      cliente.setNumCell(txtNumCel.getText());

      cliente.setNumTel(txtNumTel.getText());

      cliente.setPatenteGuida(txtPatGuida.getText());

      cliente.setPartitaIva(txtPartIva.getText());

      cliente.setEmail(txtEmail.getText());

      return cliente;
    }
  }
  @Test
  public void dateIsConvertedToEntityAttribute() throws Exception {
    int expectedYear = 2015;
    Month expectedMonth = Month.APRIL;
    int expectedDayOfMonth = 15;
    // a lot of fun with old API
    Date date = new Date(expectedYear - 1900, expectedMonth.getValue() - 1, expectedDayOfMonth);

    LocalDate actualLocalDate = converter.convertToEntityAttribute(date);

    assertThat(actualLocalDate.getYear()).isEqualTo(expectedYear);
    assertThat(actualLocalDate.getMonth()).isEqualTo(expectedMonth);
    assertThat(actualLocalDate.getDayOfMonth()).isEqualTo(expectedDayOfMonth);
  }
Ejemplo n.º 6
0
  private static Birthday toBirthday(final ResultSet rs, final int rowNum) throws SQLException {
    final Birthday birthday = new Birthday();
    birthday.setId(rs.getString("MIG_ID").trim());
    birthday.setName(String.join(" ", rs.getString("MIG_VORNAME"), rs.getString("MIG_NACHNAME")));

    final LocalDate birthdate = asLocalDate(rs.getDate("MIG_GEB_DAT"));
    if (birthdate != null) {
      birthday.setDateOfBirth(birthdate.toString());
      birthday.setMonth(birthdate.getMonth().getValue());
      birthday.setDay(birthdate.getDayOfMonth());
      birthday.setAge(ageOf(birthdate));
    }

    return birthday;
  }
Ejemplo n.º 7
0
  static void f1() {
    LocalDate date = LocalDate.of(2016, Month.JANUARY, 24);
    int year = date.getYear(); // 2016
    Month month = date.getMonth(); // 1月
    int dom = date.getDayOfMonth(); // 24
    DayOfWeek dow = date.getDayOfWeek(); // 星期天,SUNDAY
    int len = date.lengthOfMonth(); // 31 (1月份的天数)
    boolean leap = date.isLeapYear(); // true (是闰年)

    date = date.withYear(2015); // 2015-01-24
    date = date.plusMonths(2); // 2015-03-24
    date = date.minusDays(1); // 2015-03-23

    System.out.println(dow);
  }
Ejemplo n.º 8
0
  private static void useLocalDate() {
    LocalDate date = LocalDate.of(2014, 3, 18);
    int year = date.getYear(); // 2014
    Month month = date.getMonth(); // MARCH
    int day = date.getDayOfMonth(); // 18
    DayOfWeek dow = date.getDayOfWeek(); // TUESDAY
    int len = date.lengthOfMonth(); // 31 (days in March)
    boolean leap = date.isLeapYear(); // false (not a leap year)
    System.out.println(date);

    int y = date.get(ChronoField.YEAR);
    int m = date.get(ChronoField.MONTH_OF_YEAR);
    int d = date.get(ChronoField.DAY_OF_MONTH);

    LocalTime time = LocalTime.of(13, 45, 20); // 13:45:20
    int hour = time.getHour(); // 13
    int minute = time.getMinute(); // 45
    int second = time.getSecond(); // 20
    System.out.println(time);

    LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20); // 2014-03-18T13:45
    LocalDateTime dt2 = LocalDateTime.of(date, time);
    LocalDateTime dt3 = date.atTime(13, 45, 20);
    LocalDateTime dt4 = date.atTime(time);
    LocalDateTime dt5 = time.atDate(date);
    System.out.println(dt1);

    LocalDate date1 = dt1.toLocalDate();
    System.out.println(date1);
    LocalTime time1 = dt1.toLocalTime();
    System.out.println(time1);

    Instant instant = Instant.ofEpochSecond(44 * 365 * 86400);
    Instant now = Instant.now();

    Duration d1 = Duration.between(LocalTime.of(13, 45, 10), time);
    Duration d2 = Duration.between(instant, now);
    System.out.println(d1.getSeconds());
    System.out.println(d2.getSeconds());

    Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
    System.out.println(threeMinutes);

    JapaneseDate japaneseDate = JapaneseDate.from(date);
    System.out.println(japaneseDate);
  }
 private void check(Data data) {
   LocalDate date = data.getDate();
   double price = bill.getTotalPrice();
   if (date.getYear() > 2015
       && date.getMonth().equals(Month.SEPTEMBER)
       && date.getDayOfMonth() < 8
       && date.getDayOfWeek().equals(DayOfWeek.SATURDAY)) {
     price *= 1;
     bill.setTotalPrice(price);
     logger.info(resourceBundle.getString("noTips"));
     bill.setTips(resourceBundle.getString("noTips"));
   } else {
     double tips = 0.05 * bill.getTotalPrice();
     price *= 1.05;
     bill.setTotalPrice(price);
     logger.info(resourceBundle.getString("payForTips") + tips);
     bill.setTips(resourceBundle.getString("payForTips") + " " + String.format(" %.2f ", tips));
   }
 }
Ejemplo n.º 10
0
  @Override
  protected void onNewDay(LocalDate prevDay, LocalDate newDay) throws Exception {
    if (!newDay.isAfter(getTradingStart().toLocalDate())) return;
    if (newDay.isAfter(getTradingStop().toLocalDate())) return;

    if (delay > 0) {
      --delay;
      return;
    }

    // Manage assets on first day of the month open
    if (prevDay.getMonth().equals(newDay.getMonth())) return;

    // Manage assets only on Monday's open
    // if(Calendar.toWeek(prevDay).equals(Calendar.toWeek(newDay))) return;

    // Sort the instruments
    Status[] longStatuses = statuses_.values().toArray(new Status[statuses_.size()]);
    Arrays.sort(
        longStatuses, (s1, s2) -> -Double.compare(s1.longScores.get(0), s2.longScores.get(0)));

    // Reset
    statuses_
        .values()
        .forEach(
            (s) -> {
              s.desiredPosition = 0;
            });

    String longLine = "";
    String shortLine = "";

    int end = Math.min(ntop, longStatuses.length);
    for (int ii = 0; ii < end; ++ii) {
      Status status = longStatuses[ii];
      double roc = status.roc.last();
      String str = String.format("%s: %.2f", status.getSymbol(), roc);
      if (longLine.isEmpty()) longLine = str;
      else longLine += "; " + str;

      if (roc > 0) status.desiredPosition = 1;
    }

    if (!longLine.isEmpty()) {
      longLine = "Long: " + longLine + "; ";
    }

    // Cancel all active orders.
    broker.cancelAllOrders();

    // Save the end equity
    double endEquity = getAccount().getEndEquity();
    double positionSize = endEquity / end;

    for (Status ss : longStatuses) {
      long pos = 0;
      if (ss.position > 0) pos = 1;
      else if (ss.position < 0) pos = -1;
      if (pos != ss.desiredPosition) {
        if (pos == 0) {
          if (ss.desiredPosition > 0) {
            long qty = (long) (positionSize / ss.lastClose);
            enterLong(ss.getSymbol(), qty);
          } else {
            long qty = (long) (positionSize / ss.lastClose);
            enterShort(ss.getSymbol(), qty);
          }
        } else if (pos > 0) {
          exitLong(ss.getSymbol());
          if (ss.desiredPosition < 0) {
            long qty = (long) (positionSize / ss.lastClose);
            enterShort(ss.getSymbol(), qty);
          }
        } else {
          exitShort(ss.getSymbol());
          if (ss.desiredPosition > 0) {
            long qty = (long) (positionSize / ss.lastClose);
            enterLong(ss.getSymbol(), qty);
          }
        }
      }
    }

    if (!shortLine.isEmpty()) {
      shortLine = "Short: " + shortLine + "; ";
    }

    if (!longLine.isEmpty() || !shortLine.isEmpty()) {
      System.out.println(
          String.format("%1$tY-%1$tm-%1$td [%1$ta]: %2$s%3$s", newDay, longLine, shortLine));
    }
  }
 public static long toTimeStamp(final LocalDate localDate) {
   final LocalDateTime localDateTime =
       LocalDateTime.of(
           localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0, 0, 0);
   return DateTime.toTimeStamp(localDateTime);
 }
Ejemplo n.º 12
0
  public static Month getMonth(Date date) {
    LocalDate localDate = date.toLocalDate();

    return localDate.getMonth();
  }