How does list get the month within a time interval
This article mainly explains "how to get the month within a time interval by list". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to get the month within a time interval" together.
Dependence:
Joda-time joda-time 2.10.5import org.joda.time.DateTime;import java.util.ArrayList;import java.util.List;public class Client {public static void main (String [] args) {Long startTime = 1599148799000L; Long endTime=1599148799000L; DateTime start = new DateTime (startTime); DateTime end = new DateTime (endTime); int oneYear = start.getYear (); int oneMonth = start.getMonthOfYear () Int twoYear = end.getYear (); int twoMonth = end.getMonthOfYear (); List yearMonth = getYearMonth (oneYear,twoYear,oneMonth,twoMonth); System.out.println (yearMonth);} private static List getYearMonth (int oneYear,int twoYear, int oneMonth,int twoMonth) {return getYearMonth (oneYear,twoYear,oneMonth,twoMonth, "-") } private static List getYearMonth (int oneYear,int twoYear, int oneMonth,int twoMonth,String separator) {List yearMonth = new ArrayList (); if (oneYear < twoYear) {if (oneYear + 1 = = twoYear) {/ / two years are relational yearMonth.addAll (getYearMonth (oneYear,separator,oneMonth,12)); yearMonth.addAll (getYearMonth (twoYear,separator,1,twoMonth)) Return yearMonth;} else {while (oneYear < twoYear) {yearMonth.addAll (getYearMonth (oneYear,separator,1,12)); oneYear + = 1;} yearMonth.addAll (getYearMonth (twoYear,separator,1,twoMonth)) }} else {yearMonth.addAll (getYearMonth (twoYear,separator,oneMonth,twoMonth));} return yearMonth;} private static List getYearMonth (int year,String separator, int fromMonth,int toMonth) {List yearMonth = new ArrayList (); for (int I = fromMonth; I