Java Data Type How to - Format date as '201506'








Question

We would like to know how to format date as '201506'.

Answer

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/*  w  w w  .j  a  v a 2 s  .  com*/
public class Main {

  public static void main(String[] args) {

    System.out.println(toUUUUMM(LocalDate.now()));
  }

  public static String toUUUUMM(LocalDate localDate) {
    return localDate.format(DateTimeFormatter.ofPattern("uuuuMM"));
  }
}

The code above generates the following result.