Calendar c = CalendargetInstance();
Systemoutprintln(cgetTime());
cset(CalendarYEAR, 2014);
cset(CalendarMONTH, CalendarDECEMBER);
cset(CalendarDATE, 29);
Systemoutprintln(cgetTime());
cset(CalendarDATE,35);
Systemoutprintln(cgetTime());
是这个意思吗?
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即可
获取本周一
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包 你需要下载下 就可以使用
以上就是关于利用java计算上上周,上周,本周,下周的起止时间(可实现跨年,跨月)全部的内容,包括:利用java计算上上周,上周,本周,下周的起止时间(可实现跨年,跨月)、Java判断日期是否是本日,本周,本月、java 如何获取 上周一日期,上周末日期,本周一日期。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)