標籤:java for ar new ad ef on sp
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //擷取前月的第一天 Calendar cal_1=Calendar.getInstance();//擷取當前日期 cal_1.add(Calendar.MONTH, -1); cal_1.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天 firstDay = format.format(cal_1.getTime()); System.out.println("-----1------firstDay:"+firstDay); //擷取前月的最後一天 Calendar cale = Calendar.getInstance(); cale.set(Calendar.DAY_OF_MONTH,0);//設定為1號,當前日期既為本月第一天 lastDay = format.format(cale.getTime()); System.out.println("-----2------lastDay:"+lastDay); //擷取當前月第一天: Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH,1);//設定為1號,當前日期既為本月第一天 String first = format.format(c.getTime()); System.out.println("===============first:"+first); //擷取當前月最後一天 Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String last = format.format(ca.getTime()); System.out.println("===============last:"+last);