java中怎样获取本月第一个星期一
3个回答
展开全部
import java.util.Calendar;
import java.util.Date;
public class C {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int i = 1;
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
cal.set(Calendar.DAY_OF_MONTH, i++);
}
Date firstMonday = cal.getTime();
System.out.println(firstMonday.toString());
}
}
import java.util.Date;
public class C {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int i = 1;
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
cal.set(Calendar.DAY_OF_MONTH, i++);
}
Date firstMonday = cal.getTime();
System.out.println(firstMonday.toString());
}
}
追问
可不可以 不是输出这个了:? Mon Jul 04 13:01:24 CST 2011
输出的是这样:2011-7-04
追答
Easy!
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class C {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int i = 1;
while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
cal.set(Calendar.DAY_OF_MONTH, i++);
}
Date firstMonday = cal.getTime();
String dtStr = new SimpleDateFormat("yyyy-MM-dd").format(firstMonday);
System.out.println(dtStr);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询