英文:
How to get list of passed months of this year java?
问题
@SuppressLint("SimpleDateFormat")
private void handleXAxis() {
List<String> allDates = new ArrayList<>();
String maxDate = "Jan";
SimpleDateFormat monthDate = new SimpleDateFormat("MMM");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(Objects.requireNonNull(monthDate.parse(maxDate)));
} catch (ParseException e) {
e.printStackTrace();
}
for (int i = 1; i <= 12; i++) {
String month_name1 = monthDate.format(cal.getTime());
allDates.add(month_name1);
cal.add(Calendar.MONTH, -1);
}
}
英文:
I am using SingleLine chart from PhilJay/MPAndroidChart android library and i need a list of passed months of current year. So for example from Jan to Oct, but when is October pass then from Jan to Nov and so on.
I tried these: Getting List of Month for Past 1 year in Android dynamically,
and Calculate previous 12 months from given month - SimpleDateFormat
but all of these are for previosly 12 months and i want from start of current year
@SuppressLint("SimpleDateFormat")
private void handleXAxis() {
List<String> allDates = new ArrayList<>();
String maxDate = "Jan";
SimpleDateFormat monthDate = new SimpleDateFormat("MMM");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(Objects.requireNonNull(monthDate.parse(maxDate)));
} catch (ParseException e) {
e.printStackTrace();
}
for (int i = 1; i <= 12; i++) {
String month_name1 = monthDate.format(cal.getTime());
allDates.add(month_name1);
cal.add(Calendar.MONTH, -1);
}
}
答案1
得分: 4
截至当前月份(包括当前月份)的YearMonth
列表:
public static List<YearMonth> getMonthsOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<YearMonth> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(YearMonth.of(currentMonth.getYear(), month));
}
return yearMonths;
}
截至当前月份(包括当前月份)的String
列表:
public static List<String> getMonthNamesOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<String> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(YearMonth.of(currentMonth.getYear(), month)
.format(DateTimeFormatter.ofPattern("MMM",
Locale.ENGLISH)));
}
return yearMonths;
}
作为另一种选择,您可以使用Month
的显示名称,而不是使用DateTimeFormatter.ofPattern("MMM")
:
public static List<String> getMonthNamesOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<String> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(Month.of(month)
.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
}
return yearMonths;
}
第二和第三个示例的输出:
一月,二月,三月,四月,五月,六月,七月,八月,九月,十月
当像这样调用时:
System.out.println(String.join(", ", getMonthNamesOfCurrentYear()));
英文:
tl;dr ⇒ java.time
Months until (including) the current one as List<YearMonth>
:
public static List<YearMonth> getMonthsOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<YearMonth> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(YearMonth.of(currentMonth.getYear(), month));
}
return yearMonths;
}
Months until (including) the current one as List<String>
:
public static List<String> getMonthNamesOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<String> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(YearMonth.of(currentMonth.getYear(), month)
.format(DateTimeFormatter.ofPattern("MMM",
Locale.ENGLISH)));
}
return yearMonths;
}
as an alternative, you can use the display name of the Month
instead of using a DateTimeFormatter.ofPattern("MMM")
public static List<String> getMonthNamesOfCurrentYear() {
YearMonth currentMonth = YearMonth.now();
List<String> yearMonths = new ArrayList<>();
for (int month = 1; month <= currentMonth.getMonthValue(); month++) {
yearMonths.add(Month.of(month)
.getDisplayName(TextStyle.SHORT, Locale.ENGLISH));
}
return yearMonths;
}
The output of the second and third example:
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct
when invoked like
System.out.println(String.join(", ", getMonthNamesOfCurrentYear()));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论