import javatextSimpleDateFormat;
import javautilCalendar;
public class WeekDemo {
public static void main(String[] args) {
printWeeks();
}
public static void printWeeks() {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = CalendargetInstance();
calendarset(CalendarDATE, 1);
int month = calendarget(CalendarMONTH);
int count = 0;
while (calendarget(CalendarMONTH) == month) {
if (calendarget(CalendarDAY_OF_WEEK) == CalendarMONDAY) {
StringBuilder builder = new StringBuilder();
builderappend("week:");
builderappend(++count);
builderappend(" (");
builderappend(formatformat(calendargetTime()));
builderappend(" - ");
calendaradd(CalendarDATE, 6);
builderappend(formatformat(calendargetTime()));
builderappend(")");
Systemoutprintln(buildertoString());
}
calendaradd(CalendarDATE, 1);
}
}
}
获取本周一
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 如何获取 上周一日期,上周末日期,本周一日期。、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)