日期:2014-05-17 浏览次数:20869 次
import java.text.SimpleDateFormat; import java.util.Calendar; public class T2 { public static void main(String[] args) { Calendar c = Calendar.getInstance(); int m = c.get(Calendar.MONTH) + 1; //月份 int s = (int)Math.ceil( (m - 1) / 3) + 1; //季度 System.out.println(s); int em = (s - 1) * 3; //上一个季度最后一个月 Calendar sc = Calendar.getInstance(); sc.setTimeInMillis(c.getTimeInMillis()); sc.set(Calendar.MONTH, em); sc.set(Calendar.DAY_OF_MONTH, 1); sc.set(Calendar.HOUR_OF_DAY, 0); sc.set(Calendar.MINUTE, 0); sc.set(Calendar.SECOND, 0); sc.set(Calendar.MILLISECOND, 0); sc.add(Calendar.MILLISECOND, -1); out(sc);//结束时间 Calendar ec = Calendar.getInstance(); ec.setTimeInMillis(c.getTimeInMillis()); ec.set(Calendar.MONTH, em - 1); //month 基于0,一月的值是0 ec.set(Calendar.DAY_OF_MONTH, 1); ec.set(Calendar.HOUR_OF_DAY, 0); ec.set(Calendar.MINUTE, 0); ec.set(Calendar.SECOND, 0); ec.set(Calendar.MILLISECOND, 0); ec.add(Calendar.MONTH, -11); //向前12个月,但是-11 out(ec);//开始时间 } protected static void out(Calendar c){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = sdf.format(c.getTime()); System.out.println(str); } }