Java, DateTimeFormatter

To format Java 8 LocalDate, LocalTime or LocalDateTime. We can use DateTimeFormatter.

Month

public static void main(String[] args) {
		
  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("M dd, yyyy, hh:mm");
  DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MM dd, yyyy, hh:mm");
  DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("MMM dd, yyyy, hh:mm");
  DateTimeFormatter dtf4 = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm");
		
  System.out.println(dtf1.format(LocalDateTime.now()));
  System.out.println(dtf2.format(LocalDateTime.now()));
  System.out.println(dtf3.format(LocalDateTime.now()));
  System.out.println(dtf4.format(LocalDateTime.now()));
}

Output

7 26, 2019, 11:32
07 26, 2019, 11:32
Jul 26, 2019, 11:32
July 26, 2019, 11:32

Day

public static void main(String[] args) {
  LocalDate now = LocalDate.of(2019, Month.JULY, 1);
  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("MM d, yyyy");
  DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MM dd, yyyy");

  System.out.println(dtf1.format(now));
  System.out.println(dtf2.format(now));
}

Output

07 1, 2019,
07 01, 2019

Year

public static void main(String[] args) {
  LocalDate now = LocalDate.of(2019, Month.JULY, 1);
  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("MM d, yy");
  DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("MM dd, yyyy");

  System.out.println(dtf1.format(LocalDateTime.now()));
  System.out.println(dtf2.format(LocalDateTime.now()));
}

Output

07 26, 19
07 26, 2019

Day Of Month

public static void main(String[] args) {

  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("MM D, yy");
  System.out.println(dtf1.format(LocalDateTime.now()));

}

## Output ##
07 207, 19

Quarter of Year

public static void main(String[] args) {

  LocalDate feb = LocalDate.of(2019, Month.FEBRUARY, 1);
  LocalDate apr = LocalDate.of(2019, Month.APRIL, 1);
  LocalDate jul = LocalDate.of(2019, Month.JULY, 1);
  LocalDate nov = LocalDate.of(2019, Month.NOVEMBER, 1);
  
  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("Q");
  DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("QQ");
  DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("QQQ");
  DateTimeFormatter dtf4 = DateTimeFormatter.ofPattern("QQQQ");

  System.out.println(dtf1.format(q1));
  System.out.println(dtf2.format(q2));
  System.out.println(dtf3.format(q3));
  System.out.println(dtf4.format(q4));
}

## Output ##
1
02
Q3
4th quarter

AM/PM

public static void main(String[] args) {

  DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("MM dd, yyyy, hh:mm a");

  System.out.println(dtf1.format(q1));
}

## Output ##
07 26, 2019, 12:01 PM

LocalizedDate, LocalizedTime & LocalizeDateTime – FormatStyle.SHORT

public static void main(String[] args) {
		
  DateTimeFormatter f1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
  DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
  DateTimeFormatter f3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);

  LocalDateTime dateTime = LocalDateTime.now();
  Period period = Period.of(1, 2, 3);
  dateTime = dateTime.minus(period);

  System.out.println(f1.format(dateTime));
  System.out.println(f2.format(dateTime)); 
  System.out.println(f3.format(dateTime));
}

## Output ##
23/05/2018
2:47 PM
23/05/2018, 2:47 PM

LocalizedDate, LocalizedTime & LocalizeDateTime – FormatStyle.MEDIUM

public static void main(String[] args) {
		
  DateTimeFormatter f1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
  DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
  DateTimeFormatter f3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

  LocalDateTime dateTime = LocalDateTime.now();
  Period period = Period.of(1, 2, 3);
  dateTime = dateTime.minus(period);

  System.out.println(f1.format(dateTime));
  System.out.println(f2.format(dateTime)); 
  System.out.println(f3.format(dateTime));
}

## Output ##
23 May 2018
2:50:06 PM
23 May 2018, 2:50:06 PM

LocalizedDate, LocalizedTime & LocalizeDateTime – FormatStyle.LONG/FULL

As in documentation mention, The {@code FULL} and {@code LONG} styles typically require a time-zone. When formatting using these styles LONG/FULL, we need to provides the zoneID. Otherwise the compiler will throws DateTimeException.

public static void main(String[] args) {

  ZoneId KL = ZoneId.of("Asia/Kuala_Lumpur");
		
  DateTimeFormatter f1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withZone(KL);
  DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(KL);
  DateTimeFormatter f3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(KL);

  LocalDateTime dateTime = LocalDateTime.now();
  System.out.println(f1.format(dateTime));
  System.out.println(f2.format(dateTime)); 
  System.out.println(f3.format(dateTime));
}

## Output ##
26 July 2019
3:20:36 PM MYT
26 July 2019 at 3:20:36 PM MYT

First and Last Day of the Month

public static void main(String args[]) {
   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
   LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
   LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());

   System.out.println("firstDayOfMonth " + formatter.format(firstDayOfMonth));
   System.out.println("lastDayOfMonth " + formatter.format(lastDayOfMonth));
}

## Output ##
firstDayOfMonth 01/07/2019
lastDayOfMonth 31/07/2019

Beginning of the Day

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

   DateTimeFormatter formatter2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

   LocalDateTime beginningOfDay = LocalDate.parse("2019-01-12").atStartOfDay();
   LocalDateTime beginningOfToday = LocalDate.now().atStartOfDay();

   System.out.println("beginningOfDay " + formatter.format(beginningOfDay));
   System.out.println("beginningOfToday " + formatter.format(beginningOfToday));
   System.out.println("beginningOfDay " + formatter2.format(beginningOfDay));
   System.out.println("beginningOfToday " + formatter2.format(beginningOfToday));

}

## Output ##
beginningOfDay 12/06/2019 12:00:00 AM
beginningOfToday 26/07/2019 12:00:00 AM
beginningOfDay2 2019-06-12T00:00:00
beginningOfToday2 2019-07-26T00:00:00

Duration

Duration deal with Day, Hours, Minutes, Second, Millies and Nano

public static void main(String args[]) {

   LocalTime a = LocalTime.of(11, 59, 0);
   LocalTime b = LocalTime.parse("09:30:24");
   LocalTime c = LocalTime.now();

   LocalTime a1 = a.plus(Duration.ofMinutes(1));
   LocalTime b1 = b.plus(Duration.ofMinutes(1));
   LocalTime c1 = c.plus(Duration.ofMinutes(1));

   System.out.println(a + " " + a1);
   System.out.println(b + " " + b1);
   System.out.println(c + " " + c1);
}

## Output ##
11:59 12:00
09:30:24 09:31:24
18:42:51.474017 18:43:51.474017

Date and Calendar

public static void main(String[] args) {
  LocalDateTime date = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
  LocalDateTime calendar = LocalDateTime.ofInstant(Calendar.getInstance().toInstant(), ZoneId.systemDefault());
		
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yy");
		
  System.out.println(formatter.format(date));
  System.out.println(formatter.format(calendar));

}

## Output ##
26/07/19
26/07/19
Java, DateTimeFormatter

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.