Example usage for java.time LocalDate from

List of usage examples for java.time LocalDate from

Introduction

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

Prototype

public static LocalDate from(TemporalAccessor temporal) 

Source Link

Document

Obtains an instance of LocalDate from a temporal object.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate a = LocalDate.from(LocalDate.now());

    System.out.println(a);
}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    TemporalAccessor ta = formatter.parse("06/10/2014");
    LocalDate ld = LocalDate.from(ta);
    System.out.println(ld);/* www .  java  2s.  c  o  m*/

}

From source file:Main.java

public static void main(String[] args) {
    LocalDate february20th = LocalDate.of(2014, Month.FEBRUARY, 20);
    System.out.println(february20th);
    System.out.println(LocalDate.from(february20th.plus(15, ChronoUnit.YEARS))); // 2029-02-20
    System.out.println(LocalDate.MAX);
    System.out.println(LocalDate.MIN);

    System.out.println(LocalTime.MIDNIGHT); // 00:00
    System.out.println(LocalTime.NOON); // 12:00
    System.out.println(LocalTime.of(23, 12, 30, 500)); // 23:12:30.000000500
    System.out.println(LocalTime.now()); // 00:40:34.110
    System.out.println(LocalTime.ofSecondOfDay(11 * 60 * 60)); // 11:00
    System.out.println(LocalTime.from(LocalTime.MIDNIGHT.plusHours(4))); // 04:00
    System.out.println(LocalTime.MIN);
    System.out.println(LocalTime.MAX);

    System.out.println(LocalDateTime.of(2014, 2, 15, 12, 30, 50, 200)); // 2014-02-15T12:30:50.000000200
    System.out.println(LocalDateTime.now()); // 2014-02-28T17:28:21.002
    System.out.println(LocalDateTime.from(LocalDateTime.of(2014, 2, 15, 12, 30, 40, 500).plusHours(19))); // 2014-02-16T07:30:40.000000500
    System.out.println(LocalDateTime.MAX);
}

From source file:Main.java

public static void parseStr(DateTimeFormatter formatter, String text) {
    try {/*from w w w. j  a v  a2 s.c  om*/
        TemporalAccessor ta = formatter.parseBest(text, OffsetDateTime::from, LocalDateTime::from,
                LocalDate::from);
        if (ta instanceof OffsetDateTime) {
            OffsetDateTime odt = OffsetDateTime.from(ta);
            System.out.println("OffsetDateTime: " + odt);
        } else if (ta instanceof LocalDateTime) {
            LocalDateTime ldt = LocalDateTime.from(ta);
            System.out.println("LocalDateTime: " + ldt);
        } else if (ta instanceof LocalDate) {
            LocalDate ld = LocalDate.from(ta);
            System.out.println("LocalDate: " + ld);
        } else {
            System.out.println("Parsing returned: " + ta);
        }
    } catch (DateTimeParseException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

@Override
public Quarter queryFrom(TemporalAccessor temporal) {
    LocalDate now = LocalDate.from(temporal);

    if (now.isBefore(now.with(Month.APRIL).withDayOfMonth(1))) {
        return Quarter.FIRST;
    } else if (now.isBefore(now.with(Month.JULY).withDayOfMonth(1))) {
        return Quarter.SECOND;
    } else if (now.isBefore(now.with(Month.NOVEMBER).withDayOfMonth(1))) {
        return Quarter.THIRD;
    } else {/*from   w  w w  .  j  a v  a 2 s.c om*/
        return Quarter.FOURTH;
    }
}

From source file:Main.java

@Override
public LocalDate queryFrom(TemporalAccessor temporal) {
    LocalDate date = LocalDate.from(temporal);
    LocalDate currentYearMLKDay = getMartinLutherKingDayForDateInYear(date.getYear());

    Period periodToCurrentYearMLKDay = Period.between(date, currentYearMLKDay);

    if (periodToCurrentYearMLKDay.isNegative() || periodToCurrentYearMLKDay.isZero()) {
        return getMartinLutherKingDayForDateInYear(date.getYear() + 1);
    } else {//from  ww w.jav  a 2  s .com
        return currentYearMLKDay;
    }
}

From source file:Main.java

@Override
public Temporal adjustInto(Temporal input) {
    LocalDate date = LocalDate.from(input);
    LocalDate nextMonth = date.plusMonths(1);
    LocalDate firstTuesdayInNextMonth = nextMonth.with(TemporalAdjusters.firstInMonth(DayOfWeek.TUESDAY));
    return input.with(firstTuesdayInNextMonth);
}

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 .j  a  v  a2 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:defaultmethods.SimpleTimeClient.java

public void setTime(int hour, int minute, int second) {
    LocalDate currentDate = LocalDate.from(dateAndTime);
    LocalTime timeToSet = LocalTime.of(hour, minute, second);
    dateAndTime = LocalDateTime.of(currentDate, timeToSet);
}

From source file:be.wegenenverkeer.common.resteasy.json.Iso8601AndOthersLocalDateFormat.java

/**
 * Parse string to date./*w  w  w. j a  v  a 2  s .  com*/
 *
 * @param str string to parse
 * @return date
 */
public LocalDate parse(String str) {
    LocalDate date = null;

    if (!StringUtils.isBlank(str)) {
        // try ISO 8601 format first
        try {
            return LocalDate.from(iso8601NozoneFormat.parse(str));
        } catch (IllegalArgumentException | DateTimeParseException ex) {
            // ignore, try next format
            date = null; // dummy
        }

        // then try a list of formats
        for (DateTimeFormatter formatter : FORMATS) {
            try {
                return LocalDate.from(formatter.parse(str));
            } catch (IllegalArgumentException | DateTimeParseException e) {
                // ignore, try next format
                date = null; // dummy
            }
        }
        throw new IllegalArgumentException("Could not parse date " + str
                + " using ISO 8601 or any of the formats " + Arrays.asList(FORMATS) + ".");

    }
    return date; // empty string
}