这篇文章主要讲解了“list怎么获取一个时间间隔内月份”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“list怎么获取一个时间间隔内月份”吧!
创新互联公司-专业网站定制、快速模板网站建设、高性价比焉耆网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式焉耆网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖焉耆地区。费用合理售后完善,十余年实体公司更值得信赖。
依赖:
<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怎么获取一个时间间隔内月份这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
分享题目:list怎么获取一个时间间隔内月份
转载来源:https://www.cdcxhl.com/article30/jpodpo.html
成都网站建设公司_创新互联,为您提供标签优化、域名注册、手机网站建设、静态网站、ChatGPT、响应式网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联