Get the Current Month Name in Java

This Java tutorial shows how to get the current month name. This tutorial will use the SimpleDateFormat to get the month name which provides an extra benefit of providing localized month names.

To get only the full month name, the SimpleDateFormat class will be created with "MMMM". If you only want the abbreviated month name, you would use the string "MMM". For more information, refer to the SimpleDateFormat class documentation.

The final step of getting the current month name is to call the format() method in SimpleDateFormat. This method will return a formatted string based on the string provided to the SimpleDateFormat constructor. In this example, the format() method will return the full month name.

The following code example puts it all together to show how to get the current month name.

import java.util.Date;
import java.text.SimpleDateFormat;

...

Date today = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM");
String monthName = dateFormat.format(today);


0 comments:

Post a Comment