Java Date Time - Year format(DateTimeFormatter formatter) example








Year format(DateTimeFormatter formatter) formats this year using the specified formatter.

Syntax

format has the following syntax.

public String format(DateTimeFormatter formatter)

Example

The following example shows how to use format.

import java.time.Year;
import java.time.format.DateTimeFormatter;
/* w w  w.  j av  a2 s . c o  m*/
public class Main {
  public static void main(String[] args) {
    Year y = Year.of(2014);
    String l = y.format(DateTimeFormatter.ofPattern("yyyy"));
    System.out.println(l);

  }
}

The code above generates the following result.