Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active June 30, 2018 19:04
Show Gist options
  • Save rocboronat/6b98f48baeb7de9021e2 to your computer and use it in GitHub Desktop.
Save rocboronat/6b98f48baeb7de9021e2 to your computer and use it in GitHub Desktop.
Get the names of the months, from strings.xml or the SimpleDateFormat class
public String getDateMonthFromStrings(Context context, DateTime date) {
String result;
if (date.getMonthOfYear() == 1) {
result = context.getString(R.string.month_january);
} else if (date.getMonthOfYear() == 2) {
result = context.getString(R.string.month_february);
} else if (date.getMonthOfYear() == 3) {
result = context.getString(R.string.month_march);
} else if (date.getMonthOfYear() == 4) {
result = context.getString(R.string.month_april);
} else if (date.getMonthOfYear() == 5) {
result = context.getString(R.string.month_may);
} else if (date.getMonthOfYear() == 6) {
result = context.getString(R.string.month_june);
} else if (date.getMonthOfYear() == 7) {
result = context.getString(R.string.month_july);
} else if (date.getMonthOfYear() == 8) {
result = context.getString(R.string.month_august);
} else if (date.getMonthOfYear() == 9) {
result = context.getString(R.string.month_september);
} else if (date.getMonthOfYear() == 10) {
result = context.getString(R.string.month_october);
} else if (date.getMonthOfYear() == 11) {
result = context.getString(R.string.month_november);
} else {
result = context.getString(R.string.month_december);
}
return result;
}
private List<CharSequence> getMonths() {
ArrayList<CharSequence> months = new ArrayList<CharSequence>();
BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();
Locale locale = baseApplication.getCountrySelected().getLocale();
for (int i = 0; i < 12; i++) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMM", locale);
cal.set(Calendar.MONTH, i);
months.add(month_date.format(cal.getTime()));
}
return months;
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="month_january">January</string>
<string name="month_february">February</string>
<string name="month_march">March</string>
<string name="month_april">April</string>
<string name="month_may">May</string>
<string name="month_june">June</string>
<string name="month_july">July</string>
<string name="month_august">August</string>
<string name="month_september">September</string>
<string name="month_october">October</string>
<string name="month_november">November</string>
<string name="month_december">Dicember</string>
</resources>
@rocboronat
Copy link
Author

DateTime is part of JodaTime for Android

compile 'net.danlew:android.joda:2.7.1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment