Example usage for java.time LocalDateTime now

List of usage examples for java.time LocalDateTime now

Introduction

In this page you can find the example usage for java.time LocalDateTime now.

Prototype

public static LocalDateTime now() 

Source Link

Document

Obtains the current date-time from the system clock in the default time-zone.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDate localDate = LocalDate.now();
    System.out.println(localDate);

    LocalTime localTime = LocalTime.now();
    System.out.println(localTime);

    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println(dateTime);

    ZonedDateTime dateTimeWithZone = ZonedDateTime.now();
    System.out.println(dateTimeWithZone);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.now();
    LocalTime time = LocalTime.now();
    LocalDateTime dateTimeFromDateAndTime = LocalDateTime.of(date, time);

    System.out.println(dateTimeFromDateAndTime);

    LocalDate dateFromDateTime = LocalDateTime.now().toLocalDate();
    LocalTime timeFromDateTime = LocalDateTime.now().toLocalTime();

    System.out.println(dateFromDateTime);

    System.out.println(timeFromDateTime);
}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY)
            .appendLiteral(":").appendValue(ChronoField.MINUTE_OF_HOUR).toFormatter();

    System.out.println(formatter.format(LocalDateTime.now()));
}

From source file:Main.java

public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);/*from w  w w  .ja  v  a  2  s . c om*/

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
        ZoneId zone = ZoneId.of(s);
        ZonedDateTime zdt = dt.atZone(zone);
        ZoneOffset offset = zdt.getOffset();
        String out = String.format("%35s %10s%n", zone, offset);
        System.out.println(out);
    }
}

From source file:Main.java

public static void main(String[] argv) {
    String dayAfterTommorrow = "20140116";
    LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE);
    System.out.printf("Date generated from String %s is %s %n", dayAfterTommorrow, formatted);
    System.out.println("Formatted today = " + DateTimeFormatter.BASIC_ISO_DATE.format(LocalDateTime.now()));

}

From source file:Main.java

public static void main(String[] argv) {
    LocalDate today = LocalDate.now();
    LocalDate tomorrow = today.plusDays(1);
    LocalDateTime time = LocalDateTime.now();
    LocalDateTime nextHour = time.plusHours(1);

    System.out.println("today = " + today);
    System.out.println("1) tomorrow = " + tomorrow + " \n2) tomorrow = " + today.plus(1, DAYS));
    System.out.println("local time now = " + time);
    System.out.println("1) nextHour = " + nextHour + " \n2) nextHour = " + time.plus(1, HOURS));

    LocalDate nextWeek = today.plus(1, WEEKS);
    System.out.println("Date after 1 week : " + nextWeek);

    LocalDate previousYear = today.minus(1, YEARS);
    System.out.println("Date before 1 year : " + previousYear);

    LocalDate nextYear = today.plus(1, YEARS);
    System.out.println("Date after 1 year : " + nextYear);

    LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
    System.out.println("firstDayOfMonth = " + firstDayOfMonth);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate date = LocalDate.now();
    // default format
    System.out.println("Default format of LocalDate=" + date);
    // specific format
    System.out.println(date.format(DateTimeFormatter.ofPattern("d::MMM::uuuu")));
    System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));

    LocalDateTime dateTime = LocalDateTime.now();
    // default format
    System.out.println("Default format of LocalDateTime=" + dateTime);
    // specific format
    System.out.println(dateTime.format(DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss")));
    System.out.println(dateTime.format(DateTimeFormatter.BASIC_ISO_DATE));

    Instant timestamp = Instant.now();
    // default format
    System.out.println("Default format of Instant=" + timestamp);

    // Parse examples
    LocalDateTime dt = LocalDateTime.parse("27::Apr::2014 21::39::48",
            DateTimeFormatter.ofPattern("d::MMM::uuuu HH::mm::ss"));
    System.out.println("Default format after parsing = " + dt);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate tomorrow = LocalDate.now().plusDays(1);
    System.out.println(tomorrow);

    LocalDate yesterday = LocalDate.now().minusDays(1);
    System.out.println(yesterday);

    LocalDate lastWeek = LocalDate.now().minusWeeks(1);
    System.out.println(lastWeek);

    LocalDate nextYear = LocalDate.now().plusYears(1);
    System.out.println(nextYear);

    LocalDateTime inThreeHoursAndTwentyMinutes = LocalDateTime.now().plusHours(3).plusMinutes(20);
    System.out.println(inThreeHoursAndTwentyMinutes);
}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);
    scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {

        throw new RuntimeException(" scheduleAtFixedRate test ScheduledThreadPoolExecutor");
    }, 0, 3000, TimeUnit.MILLISECONDS);

    scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {

        System.out.println("scheduleAtFixedRate: " + LocalDateTime.now().format(formatter));
    }, 0, 2000, TimeUnit.MILLISECONDS);

    scheduledThreadPoolExecutor.schedule(() -> {

        System.out.println("schedule");
    }, 1, TimeUnit.SECONDS);/*from w  w  w.j  a  v a  2 s  . co m*/
}

From source file:Main.java

public static void main(String[] args) {

    // the current date
    LocalDate currentDate = LocalDate.now();

    // 2014-02-10
    LocalDate tenthFeb2014 = LocalDate.of(2014, Month.FEBRUARY, 10);

    // months values start at 1 (2014-08-01)
    LocalDate firstAug2014 = LocalDate.of(2014, 8, 1);

    // the 65th day of 2010 (2010-03-06)
    LocalDate sixtyFifthDayOf2010 = LocalDate.ofYearDay(2010, 65);

    // times, e.g. 19:12:30.733

    LocalTime currentTime = LocalTime.now(); // current time
    LocalTime midday = LocalTime.of(12, 0); // 12:00
    LocalTime afterMidday = LocalTime.of(13, 30, 15); // 13:30:15

    // 12345th second of day (03:25:45)
    LocalTime fromSecondsOfDay = LocalTime.ofSecondOfDay(12345);

    // dates with times, e.g. 2014-02-18T19:08:37.950
    LocalDateTime currentDateTime = LocalDateTime.now();

    // 2014-10-02 12:30
    LocalDateTime secondAug2014 = LocalDateTime.of(2014, 10, 2, 12, 30);

    // 2014-12-24 12:00
    LocalDateTime christmas2014 = LocalDateTime.of(2014, Month.DECEMBER, 24, 12, 0);

    // current (local) time in Los Angeles
    LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));

    // current time in UTC time zone
    LocalTime nowInUtc = LocalTime.now(Clock.systemUTC());

    System.out.println("date/time creation: currentDate: " + currentDate);
    System.out.println("date/time creation: tenthFeb2014: " + tenthFeb2014);
    System.out.println("date/time creation: firstAug2014: " + firstAug2014);
    System.out.println("date/time creation: sixtyFifthDayOf2010: " + sixtyFifthDayOf2010);
    System.out.println("date/time creation: currentTime: " + currentTime);
    System.out.println("date/time creation: midday: " + midday);
    System.out.println("date/time creation: afterMidday: " + afterMidday);
    System.out.println("date/time creation: fromSecondsOfDay: " + fromSecondsOfDay);
    System.out.println("date/time creation: currentTimeInLosAngeles: " + currentTimeInLosAngeles);
    System.out.println("date/time creation: currentDateTime: " + currentDateTime);
    System.out.println("date/time creation: secondAug2014: " + secondAug2014);
    System.out.println("date/time creation: christmas2014: " + christmas2014);
}