您好,登錄后才能下訂單哦!
這篇文章主要講解了“list怎么獲取一個時間間隔內月份”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“list怎么獲取一個時間間隔內月份”吧!
依賴:
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.10.5</version> </dependency>
import 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<String> yearMonth = getYearMonth(oneYear,twoYear,oneMonth,twoMonth); System.out.println(yearMonth); } private static List<String> getYearMonth(int oneYear,int twoYear ,int oneMonth,int twoMonth){ return getYearMonth(oneYear,twoYear,oneMonth,twoMonth,"-"); } private static List<String> getYearMonth(int oneYear,int twoYear ,int oneMonth,int twoMonth,String separator){ List<String> yearMonth = new ArrayList<>(); if(oneYear < twoYear){ if(oneYear + 1 == twoYear){// 兩個年份是間隔為1的關系 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<String> getYearMonth(int year,String separator ,int fromMonth,int toMonth){ List<String> yearMonth = new ArrayList<>(); for (int i = fromMonth; i <= toMonth; i++) { if(i< 10){ yearMonth.add(year+separator+"0"+i); }else { yearMonth.add(year+separator+i); } } return yearMonth; } }
結果
[2020-09]
感謝各位的閱讀,以上就是“list怎么獲取一個時間間隔內月份”的內容了,經過本文的學習后,相信大家對list怎么獲取一個時間間隔內月份這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。