是这样的,我想根据日期生成以前某个月份(例如2005年1月)到现在(也就是本年本月)的一个期号,期号的格式为年份+月份再加某值,例如20080805,前面四位由年份和月份组成,请问如何生成?
------解决方案--------------------
没太明白你的意思 是像杂志一样 从以前的某天到现在 每隔N天出一期 算到现在出到多少期?
你的20080805可以用:当前年*10000+当前月*100+某值 得到的是int 可以转化为别的类型
只是不明白你的“某值”是什么
------解决方案--------------------
- Java code
public static void main(String[] args) { Calendar preDate = Calendar.getInstance(); Calendar now = Calendar.getInstance(); preDate.set(2007, 0, 1);// 月份从0开始 String year = (now.get(Calendar.YEAR) - preDate.get(Calendar.YEAR)) + ""; String month = (now.get(Calendar.MONTH) - preDate.get(Calendar.MONTH)) + ""; year = year.length() == 1 ? "0" + year : year; month = month.length() == 1 ? "0" + month : month; System.out.println(year + month);}
------解决方案--------------------
SimpleDateFormat 名字 = new SimpleDateFormat("yyyyMMdd");
System.out.println(名字.format(new Date()));
生成的就是20080821