获取今年已经过去的月份列表,Java 怎么做?

huangapple go评论85阅读模式
英文:

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(&quot;SimpleDateFormat&quot;)
private void handleXAxis() {
    List&lt;String&gt; allDates = new ArrayList&lt;&gt;();
    String maxDate = &quot;Jan&quot;;
    SimpleDateFormat monthDate = new SimpleDateFormat(&quot;MMM&quot;);
    Calendar cal = Calendar.getInstance();
    try {
        cal.setTime(Objects.requireNonNull(monthDate.parse(maxDate)));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    for (int i = 1; i &lt;= 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 &rArr; java.time

Months until (including) the current one as List&lt;YearMonth&gt;:

public static List&lt;YearMonth&gt; getMonthsOfCurrentYear() {
	YearMonth currentMonth = YearMonth.now();
	List&lt;YearMonth&gt; yearMonths = new ArrayList&lt;&gt;();
	
	for (int month = 1; month &lt;= currentMonth.getMonthValue(); month++) {
		yearMonths.add(YearMonth.of(currentMonth.getYear(), month));
	}
	
	return yearMonths;
}

Months until (including) the current one as List&lt;String&gt;:

public static List&lt;String&gt; getMonthNamesOfCurrentYear() {
	YearMonth currentMonth = YearMonth.now();
	List&lt;String&gt; yearMonths = new ArrayList&lt;&gt;();
	
	for (int month = 1; month &lt;= currentMonth.getMonthValue(); month++) {
		yearMonths.add(YearMonth.of(currentMonth.getYear(), month)
								.format(DateTimeFormatter.ofPattern(&quot;MMM&quot;,
                                                                    Locale.ENGLISH)));
	}
	
	return yearMonths;
}

as an alternative, you can use the display name of the Month instead of using a DateTimeFormatter.ofPattern(&quot;MMM&quot;)

public static List&lt;String&gt; getMonthNamesOfCurrentYear() {
	YearMonth currentMonth = YearMonth.now();
	List&lt;String&gt; yearMonths = new ArrayList&lt;&gt;();
	
	for (int month = 1; month &lt;= 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(&quot;, &quot;, getMonthNamesOfCurrentYear()));

huangapple
  • 本文由 发表于 2020年10月6日 20:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64226192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定