Salesforce Apex: How to Lookup the Name of a Month from the Date Class
Two simple solutions for this common need
The Date Class in Apex has some really nifty methods such as .isLeapYear() and daysBetween()— methods you didn’t know you needed, but you’re glad when you eventually do. Unfortunately, the quality of life methods exclude one that returns the name of the month. The most we have at our disposal is the numeric month value, but that’s not always sufficient.
In this article, we’ll discuss two strategies to lookup the name of a month from a Date Class instance. Each technique has their own pros and cons, so it’s up to you to pick the one you prefer.
The DateTime Class
If you’ve worked with the DateTime Class in Salesforce then you may be familiar with the .format() method. This method accepts a string allowing you to construct a formatted string or simply request a single portion of the timestamp.
Using a DateTime Class instance, the month name is retrieved with the following simple code.DateTime dt = DateTime.now();
System.debug(dt.format('MMMM'));
The Date Class also has a .format() method; however, it does not allow a supplied string. Furthermore, there is no .monthName() method like there are .day(), .month(), and .year().
We’ll go over two strategies in this article. Whichever one you prefer is your own choice.
Strategy 1: Convert to DateTime
Since the DateTime Class has what we want, a natural response is to simply convert our Date instance to a DateTime instance. To do so, we pass the year, month, and day into the DateTime.newInstance() method.Date d = Date.today();Integer y = d.year();
Integer m = d.month();
Integer d = d.day();DateTime dt = DateTime.newInstance(y,m,d);System.debug(dt.format('MMMM')); // Will print the name of the month
This works well as a “one-off” solution — it’s straightforward and takes advantage of preexisting methods within the Classes. This strategy will fall short if you need to convert many dates since each individual date would need to be converted to a DateTime.
Strategy 2: Custom Method
For a more repeatable strategy, let’s create a method to be used inside of our Class. We’ll define the method as private so it’s only able to be used within the Class, but this can be modified to your own needs.private String lookupMonthName(Date d) {
Map<Integer,String> monthNames = new Map<Integer,String>{
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
};try {
return monthNames.get(d.month());
} catch(NullPointerException e) {
return NULL;
}
}
There are a variety of ways to write the actual guts of the method, but I prefer this solution for the following reasons:
- Requiring a Date object prohibits an invalid month number
- Using a Map is efficient
- Null objects are handled
Naturally, this technique has the opposite pros/cons of converting to a DateTime object — it is easily reused, but for a single use it’s a bit of overkill.
Please share your experiences, questions, and feedback below. Follow Code 85 for more plain language programming guides. Thanks for reading!