java获取当前周,季度,月

1,获取当前周

Date date=new Date();

 SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");

//格式化当前日期

   try {
                weekDate = format.parse(date);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }

Calendar calendar = Calendar.getInstance();  

calendar.setFirstDayOfWeek(Calendar.MONDAY);  //美国是以周日为每周的第一天 现把周一设成第一天

calendar.setTime(date);  

System.out.println("当前周为"+calendar.get(Calendar.WEEK_OF_YEAR));  

2,获取当前月

 Calendar cal = Calendar.getInstance();
            int m = cal.get(Calendar.MONTH) + 1;

System.out.println("当前月份为"+m);  

3 ,获取当前季度

季度用的笨方法 获取了月份后 自己加的判断

   String quarter = " ";
            if (m >= 1 && m <= 3) {
                quarter = "1";
            }
            if (m >= 4 && m <= 6) {
                quarter = "2";
            }
            if (m >= 7 && m <= 9) {
                quarter = "3";
            }
            if (m >= 10 && m <= 12) {
                quarter = "4";
            }

System.out.println("当前季度为"+quarter);  

 

 

你可能感兴趣的:(java)