Here you can find the source of formatDayOfMonth(LocalDateTime dateTime)
public static String formatDayOfMonth(LocalDateTime dateTime)
//package com.java2s; //License from project: Open Source License import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static final String FORMAT_DAY_OF_MONTH = "d"; public static final String SUFFIX_FIRST = "st"; public static final String SUFFIX_SECOND = "nd"; public static final String SUFFIX_THIRD = "rd"; public static final String SUFFIX_FOURTH_ONWARDS = "th"; public static String formatDayOfMonth(LocalDateTime dateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(FORMAT_DAY_OF_MONTH); String formattedDateTime = dateTime.format(formatter); return formattedDateTime + getDayNumberSuffix(Integer.valueOf(formattedDateTime)); }//w w w. j a va2 s .c om /** * Gets the suffix for a given day number * e.g. 3 -> rd * 12 -> th * 15 -> th * 21 -> st */ public static String getDayNumberSuffix(int day) { if (day >= 11 && day <= 13) { return SUFFIX_FOURTH_ONWARDS; } switch (day % 10) { case 1: return SUFFIX_FIRST; case 2: return SUFFIX_SECOND; case 3: return SUFFIX_THIRD; default: return SUFFIX_FOURTH_ONWARDS; } } }