Example usage for java.time LocalDateTime format

List of usage examples for java.time LocalDateTime format

Introduction

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

Prototype

@Override 
public String format(DateTimeFormatter formatter) 

Source Link

Document

Formats this date-time using the specified formatter.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();

    System.out.println(now.format(DateTimeFormatter.ofPattern("dd-MM-yy HH:mm:ss")));
}

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime a = LocalDateTime.of(2014, 6, 30, 12, 00);

    String s = a.format(DateTimeFormatter.ISO_DATE);

    System.out.println(s);/*from w ww .ja  v a 2s.c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    String DATE_FORMAT = "dd/MM/yyyy HH:mm:ss";

    LocalDateTime ldt = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DATE_FORMAT);

    System.out.println(ldt.format(dtf));
}

From source file:Main.java

public static void main(String[] args) {

    DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime then = now.minusDays(7);

    System.out.println(String.format("Now:  %s\nThen: %s", now.format(format), then.format(format)));

}

From source file:Main.java

public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // french date formatting (1. avril 2014)
    String frenchDate = dateTime.format(DateTimeFormatter.ofPattern("d. MMMM yyyy", new Locale("fr")));

    System.out.println(frenchDate);
}

From source file:Main.java

public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // format as ISO week date (2014-W08-4)
    String asIsoWeekDate = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE);

    System.out.println(asIsoWeekDate);
}

From source file:Main.java

public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // using a custom pattern (01/04/2014)
    String asCustomPattern = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

    System.out.println(asCustomPattern);
}

From source file:Main.java

public static void main(String[] args) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // format ISO date time (2014-02-20T20:04:05.867)
    String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME);

    System.out.println(asIsoDateTime);
}

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) {
    // 2014-04-01 10:45
    LocalDateTime dateTime = LocalDateTime.of(2014, Month.APRIL, 1, 10, 45);

    // using short german date/time formatting (01.04.14 10:45)
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
            .withLocale(new Locale("de"));
    String germanDateTime = dateTime.format(formatter);

    System.out.println(germanDateTime);
}