public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar firstDayOfWeek = CalendargetInstance(LocalegetDefault());
firstDayOfWeeksetFirstDayOfWeek(CalendarMONDAY);
int day = firstDayOfWeekget(CalendarDAY_OF_WEEK);
firstDayOfWeekadd(CalendarDATE, -day+1+1);// 后面的+1是因为从周日开始
Systemoutprintln(formatformat(firstDayOfWeekgetTime()));
Calendar lastDayOfWeek = CalendargetInstance(LocalegetDefault());
lastDayOfWeeksetFirstDayOfWeek(CalendarMONDAY);
day = lastDayOfWeekget(CalendarDAY_OF_WEEK);
lastDayOfWeekadd(CalendarDATE, 7-day+1);
// 本周星期天的日期
Systemoutprintln(formatformat(lastDayOfWeekgetTime()));
}
这一段代码可以获取本周一的日期、本周日的日期,比如本周一为20180702、本周日为20180708。只要判断给定的日期数字是否在[20180702, 20180708]范围内即可
以上的是判断日期是否是本周的方法,至于本日和本月就很好判断了,直接将日期格式化为20180702类似的数字,然后与今天进行比较即可判断是否本日,与本月的字符串201807进行比较,看字符串的前面6个字符是否equals即可
import javautilCalendar;
import javautilDate;
public class A {
public static void main(String[] args) {
Date date=new Date();
Calendar cal = CalendargetInstance();
calsetTime(date);
Systemoutprintln(calget(CalendarDAY_OF_WEEK));
}
}
假如今天星期二,输出则为3,解释下,输出的1是从星期天开始的,也就是说,星期天为1,星期一为2,星期六为7,你可以根据这个数据减去1,就是我们日常使用的日期了。
public static void main(String[] agrs) {
String newtime="2013-8-4";
Systemoutprintln(newtime+""+testDate(newtime));
}
public static String testDate(String newtime) {
String dayNames[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int week1[] = {7,1,2,3,4,5,6};
Calendar c = CalendargetInstance();// 获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
csetTime(sdfparse(newtime));
} catch (ParseException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
String a=dayNames[cget(CalendarDAY_OF_WEEK)-1];
int b=week1[cget(CalendarDAY_OF_WEEK)-1];
if(b!=7){
cset(CalendarDAY_OF_WEEK, CalendarMONDAY);
}else{
cadd(CalendarWEEK_OF_MONTH, -1);
cset(CalendarDAY_OF_WEEK, 2);
}
for(int i=1;i<b;i++){
if(i==1){
Systemoutprintln(sdfformat(cgetTime()));//本周一
}
cadd(CalendarDATE,1);
Systemoutprintln(sdfformat(cgetTime()));
}
Systemoutprintln("-------------");
return a;
}
测试结果:
2013-07-29
2013-07-30
2013-07-31
2013-08-01
2013-08-02
2013-08-03
2013-08-04
-------------
2013-8-4星期日
获取本周一
public static Date getNowWeekMonday(Date date) {Calendar cal = CalendargetInstance();
calsetTime(date);
caladd(CalendarDAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况
calset(CalendarDAY_OF_WEEK, CalendarMONDAY);
303 return calgetTime();
}
获取上周一
public static Date getLastWeekMonday(Date date) {Date a = DateUtilsaddDays(date, -1);
Calendar cal = CalendargetInstance();
calsetTime(a);
caladd(CalendarWEEK_OF_YEAR, -1);// 一周
calset(CalendarDAY_OF_WEEK, CalendarMONDAY);
return calgetTime();
}
获取上周日
public static Date getLastWeekSunday(Date date) {
Date a = DateUtilsaddDays(date, -1);
Calendar cal = CalendargetInstance();
calsetTime(a);
calset(CalendarDAY_OF_WEEK, 1);
return calgetTime();
}
代码里面有用到 lapachecommon-ang包 你需要下载下 就可以使用
mport javatextSimpleDateFormat;
import javautilCalendar;
public class Test{
public static void main(String args[]){
Calendar cal = CalendargetInstance();
//n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推
int n = 2;
String monday;
caladd(CalendarDATE, n7);
//想周几,这里就传几CalendarMONDAY(TUESDAY)
calset(CalendarDAY_OF_WEEK,CalendarMONDAY);
monday = new SimpleDateFormat("yyyy-MM-dd")format(calgetTime());
Systemoutprintln(monday);
}
}
Calendar c = CalendargetInstance();
Systemoutprintln(cgetTime());
cset(CalendarYEAR, 2014);
cset(CalendarMONTH, CalendarDECEMBER);
cset(CalendarDATE, 29);
Systemoutprintln(cgetTime());
cset(CalendarDATE,35);
Systemoutprintln(cgetTime());
是这个意思吗?
以上就是关于Java判断日期是否是本日,本周,本月全部的内容,包括:Java判断日期是否是本日,本周,本月、java 判断今天星期几 用if else语句 有人会这个程序吗 求大神解救、java 日期,根据今天的日期,输出这一周的其他日期,例如:2013-7-31,输出这周从第一天开始到今天的日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)