Java LocalDateTime Format formatDayOfMonth(LocalDateTime dateTime)

Here you can find the source of formatDayOfMonth(LocalDateTime dateTime)

Description

format Day Of Month

License

Open Source License

Declaration

public static String formatDayOfMonth(LocalDateTime dateTime) 

Method Source Code


//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;
        }
    }
}

Related

  1. formatDate(LocalDateTime dateTime)
  2. formatDate(String inPattern, LocalDateTime inTime)
  3. formatDateFromTo(LocalDateTime dateFrom, LocalDateTime dateTo)
  4. formatDateGeneric(DateTimeFormatter dateTimeFormatter, LocalDateTime localDateTime)
  5. formatDateTime(LocalDateTime dateTime)
  6. formatLocalDateTimeToString(LocalDateTime date)
  7. formatShortDate(LocalDateTime dateTime)
  8. formattedWeek(LocalDateTime localDateTime)
  9. formatTime(LocalDateTime dateTime)