日期:2014-05-19  浏览次数:20888 次

java中根据系统时间动态获取季度以及月份
/**
* 根据系统的时间,来判断是否在每月16日-下月15日期之间,并且显示上一月的时间
 * 例如,2012年8月16日-2012年9月15日期间,就显示2012年3季度7月
 */

------解决方案--------------------
Java code


public static void main(String[] args) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        Calendar now = Calendar.getInstance();
        System.out.println(sdf.format(now.getTime()));
        Calendar time1 = new GregorianCalendar(now.get(Calendar.YEAR), now.get(Calendar.MONTH), 16);

        System.out.println(sdf.format(time1.getTime()));
        Calendar time2 = new GregorianCalendar(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, 15);
        System.out.println(sdf.format(time2.getTime()));

        if (now.after(time1) && now.before(time2)) {
            now.add(Calendar.MONTH, -1);
        } else {
            now.add(Calendar.MONTH, -2);
        }
        System.out.println("-----------------------");
        System.out.println(sdf.format(now.getTime()));
        System.out.println(now.get(Calendar.MONTH));
        System.out.println(now.get(Calendar.YEAR));
        int month = now.get(Calendar.MONTH) + 1;
        if(month == 1 || month ==2 || month ==3){
            System.out.println("一季度");
        }
        else if(month == 4 || month ==5 || month ==6){
            System.out.println("二季度");
        }
        else if(month == 7 || month ==8 || month ==9){
            System.out.println("三季度");
        }
        else if(month == 10 || month ==11 || month ==12){
            System.out.println("四季度");
        }
    }