首页 > 代码库 > java日期遍历
java日期遍历
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test0003 {
public static void main(String args[]) {
//请注意月份是从0-11
Calendar start = Calendar.getInstance();
start.set(2014,2, 1);
Calendar end = Calendar.getInstance();
end.set(2014, 3, 1);
int sumSunday = 0;
int sumSat = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
while(start.compareTo(end) <= 0) {
int w = start.get(Calendar.DAY_OF_WEEK);
if(w == Calendar.SUNDAY)
sumSunday ++;
if(w == Calendar.SATURDAY)
sumSat ++;
//打印每天
System.out.println(format.format(start.getTime()));
//循环,每次天数加1
start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);
}
System.out.println("星期天总数为:" + sumSunday);
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test0003 {
public static void main(String args[]) {
//请注意月份是从0-11
Calendar start = Calendar.getInstance();
start.set(2014,2, 1);
Calendar end = Calendar.getInstance();
end.set(2014, 3, 1);
int sumSunday = 0;
int sumSat = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
while(start.compareTo(end) <= 0) {
int w = start.get(Calendar.DAY_OF_WEEK);
if(w == Calendar.SUNDAY)
sumSunday ++;
if(w == Calendar.SATURDAY)
sumSat ++;
//打印每天
System.out.println(format.format(start.getTime()));
//循环,每次天数加1
start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);
}
System.out.println("星期天总数为:" + sumSunday);
System.out.println("星期六总数为:" + sumSat);
}
}
java日期遍历
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。