Home > Java > javaTutorial > How to Efficiently Add Ordinal Indicators (st, nd, rd, th) to Day-of-Month Numbers in Java?

How to Efficiently Add Ordinal Indicators (st, nd, rd, th) to Day-of-Month Numbers in Java?

Patricia Arquette
Release: 2024-12-20 12:16:12
Original
232 people have browsed it

How to Efficiently Add Ordinal Indicators (st, nd, rd, th) to Day-of-Month Numbers in Java?

Formatting the Day of the Month with Ordinal Indicators (11th, 21st, 23rd)

When dealing with dates, it's often necessary to output the day of the month with an ordinal indicator, such as "11th", "21st", or "23rd". While simply using the numerical day of the month can suffice, adding ordinal indicators enhances the clarity and readability of the date.

The provided Java snippet can extract the day of the month as a number using the SimpleDateFormat, but how do we format this number to include the appropriate ordinal indicator?

Solution Using Guava Library

The Guava library provides an elegant and efficient way to append ordinal indicators to the day of the month. Below is a corrected version of the provided snippet:

import static com.google.common.base.Preconditions.*;

String getDayOfMonthSuffix(final int n) {
    checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
    if (n >= 11 && n <= 13) {
        return "th";
    }
    switch (n % 10) {
        case 1:  return "st";
        case 2:  return "nd";
        case 3:  return "rd";
        default: return "th";
    }
}
Copy after login

This function takes an integer representing the day of the month and returns the appropriate ordinal indicator as a string. It uses the checkArgument method from Guava's Preconditions class to verify that the day is a valid value between 1 and 31, inclusive.

For days between 11th and 13th, the function returns "th" because these days always use the "th" suffix. For other days, it checks the last digit of the day using the modulo operation (%):

  • If the last digit is 1, it returns "st".
  • If the last digit is 2, it returns "nd".
  • If the last digit is 3, it returns "rd".
  • Otherwise, it returns "th".

Example Usage

To use this function, simply pass the day of the month as an argument. For example:

int dayOfMonth = 11;
String ordinalIndicator = getDayOfMonthSuffix(dayOfMonth); // "th"
Copy after login

Conclusion

Appending ordinal indicators to the day of the month is a common requirement in date formatting, and using the Guava library provides a straightforward and efficient solution. This improved snippet eliminates the potential bug mentioned in the original answer and ensures the correct ordinal indicator is appended to any day of the month from 1 to 31.

The above is the detailed content of How to Efficiently Add Ordinal Indicators (st, nd, rd, th) to Day-of-Month Numbers in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template