获取本周一
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包 你需要下载下 就可以使用
import javatextSimpleDateFormat;import javautilCalendar;import javautilDate;public class Main {private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");public static void main(String args[]) {Date d = new Date();// 月初Systemoutprintln("月初" + sdfformat(getMonthStart(d)));// 月末Systemoutprintln("月末" + sdfformat(getMonthEnd(d)));Date date = getMonthStart(d);Date monthEnd = getMonthEnd(d);while (!dateafter(monthEnd)) {Systemoutprintln(sdfformat(date));date = getNext(date);}}private static Date getMonthStart(Date date) {Calendar calendar = CalendargetInstance();calendarsetTime(date);int index = calendarget(CalendarDAY_OF_MONTH);calendaradd(CalendarDATE, (1 - index));return calendargetTime();}private static Date getMonthEnd(Date date) {Calendar calendar = CalendargetInstance();calendarsetTime(date);calendaradd(CalendarMONTH, 1);int index = calendarget(CalendarDAY_OF_MONTH);calendaradd(CalendarDATE, (-index));return calendargetTime();}private static Date getNext(Date date) {Calendar calendar = CalendargetInstance();calendarsetTime(date);calendaradd(CalendarDATE, 1);return calendargetTime();}} 这是一个月的你改改就能写出上个月的了吧
import javatextSimpleDateFormat;
import javautilCalendar;
public class DataTest {
public static void main(String[] args) {
printWeekdays();
}
private static final int FIRST_DAY = CalendarMONDAY;
private static void printWeekdays() {
Calendar calendar = CalendargetInstance();
setToFirstDay(calendar);
for (int i = 0; i < 7; i++) {
printDay(calendar);
calendaradd(CalendarDATE, 1);
}
}
private static void setToFirstDay(Calendar calendar) {
while (calendarget(CalendarDAY_OF_WEEK) != FIRST_DAY) {
calendaradd(CalendarDATE, -1);
}
}
private static void printDay(Calendar calendar) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd EE");
Systemoutprintln(dateFormatformat(calendargetTime()));
}
}
首先要和用户确定一周是从周几开始,不同地区可能不一样。也可以做在配置文件里可设置。
从起始周几开始编码1~7,得到周一的编码值,
获取系统当前日期,取得当前日期是周几,当前周几转换成编码,
当前日期 -(当前周几编码-周一编码)即为本周一日期。
Calendar c = CalendargetInstance();
Systemoutprintln(cgetTime());
cset(CalendarYEAR, 2014);
cset(CalendarMONTH, CalendarDECEMBER);
cset(CalendarDATE, 29);
Systemoutprintln(cgetTime());
cset(CalendarDATE,35);
Systemoutprintln(cgetTime());
是这个意思吗?
以上就是关于java 如何获取 上周一日期,上周末日期,本周一日期。全部的内容,包括:java 如何获取 上周一日期,上周末日期,本周一日期。、java获取本周所有日期,和本周第一天和最后一天,和上周所有日期,和上周第一天和上周最后一天、Java中获取本周内所有日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)