Java实现月份递减
问题:从当前月份开始,往前3年的所有月份
返回map类型,key是String,value是Date,map倒序排列
public static Map<String, Date> getDateMap() { Map<String, Date> map = new LinkedHashMap<>(); Calendar c = Calendar.getInstance(); for (int i = 0; i < 36; i++) { map.put(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月" , c.getTime()); c.add(Calendar.MONTH,-1); } return map; } public static void main(String[] args) { Map<String, Date> map = getDateMap(); for (Map.Entry<String, Date> tmp : map.entrySet()) { System.out.println(tmp.getKey()); System.out.println(tmp.getValue()); } }
原创文章,欢迎转载,转载请注明出处!
把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~