java常用日期代码
云起网2022-12-06
代码段84
//输出今天开始的日期和星期 void printWeek() throws Exception{ SimpleDateFormat dfDay = new SimpleDateFormat("yyyy-MM-dd"); String today = dfDay.format(new Date()); //String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"}; Calendar calendar = new GregorianCalendar(); calendar.setTime(dfDay.parse(today)); for(int i=0;i<7;i++){ if(i==0){ calendar.add(calendar.DATE, 0); }else { calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动 } String hkrq = dfDay.format(calendar.getTime());//格式要 yyyy-MM-dd int w = calendar.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; System.out.println(hkrq+" "+weekDays[w]); } }
很赞哦! ()