Example usage for java.time LocalDate of

List of usage examples for java.time LocalDate of

Introduction

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

Prototype

public static LocalDate of(int year, int month, int dayOfMonth) 

Source Link

Document

Obtains an instance of LocalDate from a year, month and day.

Usage

From source file:Main.java

public static void main(String[] args) {

    LocalDate today = LocalDate.now();
    LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

    LocalDate nextBDay = birthday.withYear(today.getYear());

    //If your birthday has occurred this year already, add 1 to the year.
    if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) {
        nextBDay = nextBDay.plusYears(1);
    }//from  w w  w . jav  a  2s .  c om

    Period p = Period.between(today, nextBDay);
    long p2 = ChronoUnit.DAYS.between(today, nextBDay);
    System.out.println("There are " + p.getMonths() + " months, and " + p.getDays()
            + " days until your next birthday. (" + p2 + " total)");
}

From source file:Main.java

public static void main(String[] args) {
    // current //from   w w  w .  j av  a 2  s  . co  m
    LocalDateTime localDateTime1 = LocalDateTime.now();
    System.out.println(localDateTime1);

    // 2014-06-21T16:12:34
    LocalDateTime localDateTime2 = LocalDateTime.of(2014, Month.JUNE, 21, 16, 12, 34);
    System.out.println(localDateTime2);
    // from  a  local date and  a  local  time
    LocalDate localDate1 = LocalDate.of(2014, 5, 10);
    LocalTime localTime = LocalTime.of(16, 18, 41);
    LocalDateTime localDateTime3 = LocalDateTime.of(localDate1, localTime);
    System.out.println(localDateTime3);
}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendLiteral("New Year in ")
            .appendValue(ChronoField.YEAR).appendLiteral(" is  on  ")
            .appendText(ChronoField.DAY_OF_WEEK, TextStyle.FULL_STANDALONE).toFormatter();
    LocalDate ld = LocalDate.of(2014, Month.JANUARY, 1);
    String str = ld.format(formatter);
    System.out.println(str);//w w  w .  j a  v  a 2  s.  c om

}

From source file:Main.java

public static void main(String[] args) {
    Duration duration = Duration.ofDays(33); //seconds or nanos

    Duration duration1 = Duration.ofHours(33); //seconds or nanos
    Duration duration2 = Duration.ofMillis(33); //seconds or nanos
    Duration duration3 = Duration.ofMinutes(33); //seconds or nanos
    Duration duration4 = Duration.ofNanos(33); //seconds or nanos
    Duration duration5 = Duration.ofSeconds(33); //seconds or nanos
    Duration duration6 = Duration.between(LocalDate.of(2012, 11, 11), LocalDate.of(2013, 1, 1));

    System.out.println(duration6.getSeconds());
    System.out.println(duration.getNano());
    System.out.println(duration.getSeconds());
}

From source file:Main.java

public static void main(String[] args) {
    TemporalAdjuster temporalAdjuster = (Temporal t) -> t.plus(Period.ofDays(10));

    System.out.println(temporalAdjuster);

    TemporalAdjuster fourMinutesFromNow = temporal -> temporal.plus(4, ChronoUnit.MINUTES);

    LocalTime localTime1 = LocalTime.of(12, 0, 0);
    System.out.println(localTime1.with(temporal -> temporal.plus(4, ChronoUnit.MINUTES)));

    System.out.println(Instant.now().with(temporalAdjuster));

    LocalDate localDate1 = LocalDate.of(2013, 12, 13);
    System.out.println(localDate1.with(TemporalAdjusters.lastDayOfMonth()));

}

From source file:cz.pichlik.goodsentiment.MockDataGenerator.java

public static void main(String args[]) throws IOException {
    String outputDirectory = args[0];
    LocalDate seedDate = LocalDate.of(2015, 9, 1);
    for (int i = 0; i < 100; i++) {
        LocalDate date = seedDate.plusDays(i);
        File outputDirectoryFile = new File(outputDirectory);
        outputDirectoryFile.mkdirs();// w w  w.j  a va2s.c  om
        File outputFile = new File(outputDirectoryFile, String.format("goodsentinment-data-%s.csv", date));
        generateFile(outputFile, date);
    }
}

From source file:Main.java

public static void main(String[] args) {
    TemporalQuery<Integer> daysBeforeChristmas = new TemporalQuery<Integer>() {

        public int daysTilChristmas(int acc, Temporal temporal) {
            int month = temporal.get(ChronoField.MONTH_OF_YEAR);
            int day = temporal.get(ChronoField.DAY_OF_MONTH);
            int max = temporal.with(TemporalAdjusters.lastDayOfMonth()).get(ChronoField.DAY_OF_MONTH);
            if (month == 12 && day <= 25)
                return acc + (25 - day);
            return daysTilChristmas(acc + (max - day + 1),
                    temporal.with(TemporalAdjusters.firstDayOfNextMonth()));
        }/*from   w  w w  . j  a  va 2s.  c o m*/

        @Override
        public Integer queryFrom(TemporalAccessor temporal) {
            if (!(temporal instanceof Temporal))
                throw new RuntimeException("Temporal accessor must be of type Temporal");
            return daysTilChristmas(0, (Temporal) temporal);
        }
    };

    System.out.println(LocalDate.of(2013, 12, 26).query(daysBeforeChristmas)); // 364
    System.out.println(LocalDate.of(2013, 12, 23).query(daysBeforeChristmas)); // 2
    System.out.println(LocalDate.of(2013, 12, 25).query(daysBeforeChristmas)); // 0
    System.out.println(ZonedDateTime.of(2013, 12, 1, 11, 0, 13, 938282, ZoneId.of("America/Los_Angeles"))
            .query(daysBeforeChristmas)); // 24
}

From source file:tech.tablesaw.filters.SearchPerformanceTest.java

public static void main(String[] args) throws Exception {

    Stopwatch stopwatch = Stopwatch.createStarted();

    Table t = defineSchema();//ww w . jav a2 s  .  c o  m

    generateTestData(t, numberOfRecordsInTable, stopwatch);
    t = t.sortAscendingOn("date");

    dateIndex = new LongIndex(t.dateTimeColumn("date"));
    t.setName("Observations");

    DateTimeColumn dates = t.dateTimeColumn("date");
    DoubleColumn lowValues = t.doubleColumn("lowValue");
    DoubleColumn highValues = t.doubleColumn("highValue");

    System.out.println(dates.summary());
    System.out.println(lowValues.summary());
    System.out.println(highValues.summary());

    LocalDateTime testDateTime = LocalDate.of(2010, 1, 1).atStartOfDay();
    double testLowValue = 500;
    double testHighValue = 999_500;

    stopwatch.reset();
    stopwatch.start();

    int count = 0;
    for (int i = 0; i < 1000; i++) {
        testDateTime = testDateTime.plusDays(2);
        int rowNumber = getRowNumber(t, testDateTime, testLowValue, testHighValue);
        if (rowNumber >= 0) {
            count++;
        }
    }

    stopwatch.stop();
    System.out.println("using rows with an index. found " + count + " in "
            + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
}

From source file:squash.tools.FakeBookingCreator.java

public static void main(String[] args) throws IOException {
    int numberOfDays = 21;
    int numberOfCourts = 5;
    int maxCourtSpan = 5;
    int numberOfSlots = 16;
    int maxSlotSpan = 3;
    int minSurnameLength = 2;
    int maxSurnameLength = 20;
    int minBookingsPerDay = 0;
    int maxBookingsPerDay = 8;
    LocalDate startDate = LocalDate.of(2016, 7, 5);

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    List<Booking> bookings = new ArrayList<>();
    for (LocalDate date = startDate; date.isBefore(startDate.plusDays(numberOfDays)); date = date.plusDays(1)) {
        int numBookings = ThreadLocalRandom.current().nextInt(minBookingsPerDay, maxBookingsPerDay + 1);
        List<Booking> daysBookings = new ArrayList<>();
        for (int bookingIndex = 0; bookingIndex < numBookings; bookingIndex++) {
            String player1 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));
            String player2 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic(
                    ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1));

            Set<ImmutablePair<Integer, Integer>> bookedCourts = new HashSet<>();
            daysBookings.forEach((booking) -> {
                addBookingToSet(booking, bookedCourts);
            });//from   www. j ava 2s.  c  o m

            Booking booking;
            Set<ImmutablePair<Integer, Integer>> courtsToBook = new HashSet<>();
            do {
                // Loop until we create a booking of free courts
                int court = ThreadLocalRandom.current().nextInt(1, numberOfCourts + 1);
                int courtSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxCourtSpan + 1, numberOfCourts - court + 2));
                int slot = ThreadLocalRandom.current().nextInt(1, numberOfSlots + 1);
                int slotSpan = ThreadLocalRandom.current().nextInt(1,
                        Math.min(maxSlotSpan + 1, numberOfSlots - slot + 2));
                booking = new Booking(court, courtSpan, slot, slotSpan, player1 + "/" + player2);
                booking.setDate(date.format(formatter));
                courtsToBook.clear();
                addBookingToSet(booking, courtsToBook);
            } while (Boolean.valueOf(Sets.intersection(courtsToBook, bookedCourts).size() > 0));

            daysBookings.add(booking);
        }
        bookings.addAll(daysBookings);
    }

    // Encode bookings as JSON
    // Create the node factory that gives us nodes.
    JsonNodeFactory factory = new JsonNodeFactory(false);
    // Create a json factory to write the treenode as json.
    JsonFactory jsonFactory = new JsonFactory();
    ObjectNode rootNode = factory.objectNode();

    ArrayNode bookingsNode = rootNode.putArray("bookings");
    for (int i = 0; i < bookings.size(); i++) {
        Booking booking = bookings.get(i);
        ObjectNode bookingNode = factory.objectNode();
        bookingNode.put("court", booking.getCourt());
        bookingNode.put("courtSpan", booking.getCourtSpan());
        bookingNode.put("slot", booking.getSlot());
        bookingNode.put("slotSpan", booking.getSlotSpan());
        bookingNode.put("name", booking.getName());
        bookingNode.put("date", booking.getDate());
        bookingsNode.add(bookingNode);
    }
    // Add empty booking rules array - just so restore works
    rootNode.putArray("bookingRules");
    rootNode.put("clearBeforeRestore", true);

    try (JsonGenerator generator = jsonFactory.createGenerator(new File("FakeBookings.json"),
            JsonEncoding.UTF8)) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_EMPTY);
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.writeTree(generator, rootNode);
    }
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.of(1996, Month.OCTOBER, 29);
    System.out.printf("%s%n", toString(date, JapaneseChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, MinguoChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, ThaiBuddhistChronology.INSTANCE));
    System.out.printf("%s%n", toString(date, HijrahChronology.INSTANCE));

    System.out.printf("%s%n", fromString("10/29/0008 H", JapaneseChronology.INSTANCE));
    System.out.printf("%s%n", fromString("10/29/0085 1", MinguoChronology.INSTANCE));
    System.out.printf("%s%n", fromString("10/29/2539 B.E.", ThaiBuddhistChronology.INSTANCE));
    System.out.printf("%s%n", fromString("6/16/1417 1", HijrahChronology.INSTANCE));
}