Example usage for java.time.format DateTimeFormatter format

List of usage examples for java.time.format DateTimeFormatter format

Introduction

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

Prototype

public String format(TemporalAccessor temporal) 

Source Link

Document

Formats a date-time object using this formatter.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalTime d = LocalTime.now();
    DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("H:m");

    System.out.println(TIME_FORMATTER.format(d));

}

From source file:Main.java

public static void main(String[] args) {
    LocalTime d = LocalTime.now();
    DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");

    System.out.println(TIME_FORMATTER.format(d));

}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    System.out.println(dateFormatter.format(LocalDate.now())); // Current Local Date
    System.out.println(dateFormatter.parse("Jan 19, 2014").getClass().getName()); //java.time.format.Parsed
    System.out.println(dateFormatter.parse("Jan 19, 2014", LocalDate::from)); // Jan 19, 2014

}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM,
            FormatStyle.SHORT);/*from www .  ja v  a 2  s. c  om*/
    System.out.println(dateTimeFormatter.format(LocalDateTime.now()));

}

From source file:Main.java

public static void main(String[] args) {
    DateTimeFormatter obscurePattern = DateTimeFormatter.ofPattern("MMMM dd, yyyy '(In Time Zone: 'VV')'");
    ZonedDateTime zonedNow = ZonedDateTime.now();
    System.out.println(obscurePattern.format(zonedNow));
}

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) {
    ZonedDateTime zonedDateTime = ZonedDateTime.of(2013, 1, 19, 0, 0, 0, 0, ZoneId.of("Europe/Paris"));

    DateTimeFormatter longDateTimeFormatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL).withLocale(Locale.FRENCH);
    System.out.println(longDateTimeFormatter.getLocale()); // fr
    System.out.println(longDateTimeFormatter.format(zonedDateTime));

}

From source file:Main.java

public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2014, Month.JUNE, 21);
    LocalTime lt = LocalTime.of(17, 30, 20);
    LocalDateTime ldt = LocalDateTime.of(ld, lt);

    DateTimeFormatter fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
    System.out.println("Formatter  Default Locale: " + fmt.getLocale());
    System.out.println("Short  Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    System.out.println("Medium Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
    System.out.println("Long  Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
    System.out.println("Full  Date: " + fmt.format(ld));

    fmt = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
    System.out.println("Short Time:  " + fmt.format(lt));

    fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    System.out.println("Short  Datetime: " + fmt.format(ldt));

    fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    System.out.println("Medium Datetime: " + fmt.format(ldt));

    // Use German locale to format the datetime in medius style
    fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(Locale.GERMAN);
    System.out.println(fmt.format(ldt));

    // Use Indian(English) locale to format datetime in short style
    fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(new Locale("en", "IN"));
    System.out.println(fmt.format(ldt));

    // Use Indian(English) locale to format datetime in medium style
    fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withLocale(new Locale("en", "IN"));
    System.out.println(fmt.format(ldt));

}

From source file:Main.java

public static void format(Temporal co, String pattern) {
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
    String str = fmt.format(co);
    System.out.println(pattern + ": " + str);
}

From source file:Main.java

public static void format(Temporal co, String pattern) {
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, Locale.US);
    String str = fmt.format(co);
    System.out.println(pattern + ": " + str);
}