计算给定月份的前12个月

编程入门 行业动态 更新时间:2024-10-25 11:18:50
本文介绍了计算给定月份的前12个月-SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将给定月份(从数据库中获取)的前12个月放入阵列列表中.

I am trying to get previous 12 month into an arraylist, from the given month(taken from DB).

List<String> allDates = new ArrayList<String>(); sqlQuery="select max(date) from Table_Name"; maxDate="Jan-2016"; (Result from Query);

要从maxDate获取之前的12个月,我在这里使用了SimpleDateFormat.

To get previous 12 months from maxDate,where i use SimpleDateFormat.

我想从 Given Month (maxDate)(maxDate)(而不是从当前月份)计算前12个月,我尝试了以下代码.

I want to calculate the previous 12 months from Given Month (maxDate), not from current month, i tried the following code.

// Parsing maxDate to an integer (say Jan-2016 = 0, Feb-2016= 1) Date date = new SimpleDateFormat("MMM-yyyy").parse(maxDate); Calendar cal = Calendar.getInstance(); cal.setTime(date); int month=cal.get(Calendar.MONTH); System.out.println("month : "+month); // Looping to get previous 12 months from current month. SimpleDateFormat month_date = new SimpleDateFormat("MMM-yyyy"); for (int i = 12; i > 0; i--) { Calendar calendar1 = Calendar.getInstance(); calendar1.add(Calendar.MONTH, -i); String month_name1 = month_date.format(calendar1.getTime()); allDates.add(month_name1); } System.out.println(allDates);

由于月份是从(0-11)编号的,所以我无法实现.请提出一个想法,计算给定月份的前12个月.感谢您的帮助!

Since months are numbered from (0 - 11) i couldn't achieve it. Please suggest an idea to calculate previous 12 months from given month. Appreciate your help!

推荐答案

问题是,在循环中,您总是根据当前日期而不是根据 maxDate 进行计算.

The problem is that in the loop you base the computation always from current date not from maxDate.

List<String> allDates = new ArrayList<>(); String maxDate = "Jan-2016"; SimpleDateFormat monthDate = new SimpleDateFormat("MMM-yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(monthDate.parse(maxDate)); for (int i = 1; i <= 12; i++) { String month_name1 = monthDate.format(cal.getTime()); allDates.add(month_name1); cal.add(Calendar.MONTH, -1); } System.out.println(allDates);

输出

[Jan-2016, Dec-2015, Nov-2015, Oct-2015, Sep-2015, Aug-2015, Jul-2015, Jun-2015, \ May-2015, Apr-2015, Mar-2015, Feb-2015]

编辑简短说明代码段的作用.

edit Short explanation what the snippet does.

  • 从给定的 maxDate 创建一个 Calendar ,并将其分配给 cal
  • 将 cal 中当前日期的字符串 Mon-Year 添加到列表 allDates
  • 从日历中减去一个月 cal.add(Calendar.MONTH,-1)
  • 将步骤2和3.重复十二次
  • create a Calendar from the given maxDate and assign it to cal
  • add the string Mon-Year of the current date in cal to the list allDates
  • substract one month from the calendar cal.add(Calendar.MONTH, -1)
  • repeat steps 2. and 3. twelve times
  • 如罗勒所述.如果以后要以 Date/Calendar 处理 allDates 中的值,请考虑不要在两者之间生成字符串列表.

    As mentioned by Basil. If you want to process the values in allDates later on as Date / Calendar think about not to generate a list of strings in between.

    更多推荐

    计算给定月份的前12个月

    本文发布于:2023-11-22 09:45:49,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1616939.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:个月

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!