Get the month (or day) name (localized)Tag(s): Date and Time Internationalization
Using JDK7 or less
import java.text.DateFormatSymbols; import java.util.Locale; public class DateUtils { private DateUtils() { } public static String getMonthName(int month) { return getMonthName(month, Locale.getDefault()); } public static String getMonthName(int month, Locale locale) { DateFormatSymbols symbols = new DateFormatSymbols(locale); String[] monthNames = symbols.getMonths(); return monthNames[month - 1]; } public static String getDayName(int day, Locale locale) { DateFormatSymbols symbols = new DateFormatSymbols(locale); String[] dayNames = symbols.getWeekdays(); return dayNames[day]; } public static void main(String[] args) { System.out.println(DateUtils.getMonthName(1)); System.out.println(DateUtils.getMonthName(1, new Locale("it"))); System.out.println (DateUtils.getDayName(java.util.Calendar.SUNDAY, Locale.getDefault())); /* * output : * january * gennaio * sunday */ } }
import java.time.DayOfWeek; import java.time.Instant; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.TextStyle; import java.util.Locale; public class DateDemo { public static void main (String args[]) throws Exception { ZoneId zoneId = ZoneId.systemDefault(); Instant instant = Instant.now(); ZonedDateTime zDateTime = instant.atZone(zoneId); DayOfWeek day = zDateTime.getDayOfWeek(); System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US)); System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US)); Month month = zDateTime.getMonth(); System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.US)); System.out.println(month.getDisplayName(TextStyle.NARROW, Locale.US)); System.out.println(month.getDisplayName(TextStyle.FULL, Locale.US)); /* output : Wed W Jan J January */ } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com