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();}} 这是一个月的你改改就能写出上个月的了吧
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星期日
第几周是该月的第几周吗 还是该年的第几周
下面的程序包含了2种情况, 请自行选择
package comcalender;
import javautilCalendar;
import javautilLocale;
/
输出某周的日期
演示Calendar的用法
Calendar的使用一般分2步,
1确定某天, 调用set函数
2可以取出该天的任意信息, 通过get函数
@author: zhuby
@create date:2011-1-17
/
public class WeekCalendar {
/
周计数,为全年中的周计数
@param year
@param weekCount
@return
/
public int[] getWeekDateByWeekInYear(int year,int weekCount){
Calendar cal=CalendargetInstance(LocaleCHINA);
//设置calendar的日期,此处可以确定某一天
calset(CalendarYEAR, year);
calset(CalendarWEEK_OF_YEAR, weekCount);
//获取该天的信息(理论而言,通过改变get函数的参数,可以获取该天的任意信息)
int day=calget(CalendarDAY_OF_YEAR);
int[] days=new int[7];
for(int i=0;i<7;i++){
days[i]=calget(CalendarDAY_OF_MONTH);
// Systemoutprint((calget(CalendarMONTH)+1)+"月"+calget(CalendarDAY_OF_MONTH)+"日 ");
day++;
calset(CalendarDAY_OF_YEAR, day);
}
return days;
}
/
周计数为月中的周计数
@param year
@param month
@param weekCount
@return
/
public int[] getWeekDateByWeekInMonth(int year,int month,int weekCount){
Calendar cal=CalendargetInstance(LocaleCHINA);
calset(CalendarYEAR, year);
calset(CalendarMONTH, month-1);
calset(CalendarWEEK_OF_MONTH, weekCount);
int day=calget(CalendarDAY_OF_YEAR);
int[] days=new int[7];
for(int i=0;i<7;i++){
days[i]=calget(CalendarDAY_OF_MONTH);
// Systemoutprint((calget(CalendarMONTH)+1)+"月"+calget(CalendarDAY_OF_MONTH)+"日 ");
day++;
calset(CalendarDAY_OF_YEAR, day);
}
return days;
}
public static void main(String[] args) {
WeekCalendar wc=new WeekCalendar();
for(int i=0;i<7;i++){
Systemoutprint(wcgetWeekDateByWeekInYear(2011, 7)[i]+" ");
}
Systemoutprintln();
for(int i=0;i<7;i++){
Systemoutprint(wcgetWeekDateByWeekInMonth(2011,4, 2)[i]+" ");
}
}
}
以上就是关于java获取本周所有日期,和本周第一天和最后一天,和上周所有日期,和上周第一天和上周最后一天全部的内容,包括:java获取本周所有日期,和本周第一天和最后一天,和上周所有日期,和上周第一天和上周最后一天、java 日期,根据今天的日期,输出这一周的其他日期,例如:2013-7-31,输出这周从第一天开始到今天的日期、Java中怎么获取一个月的第几周的所有日期啊比如说我要获取一月的第一周的所有天数的日期是多少要怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)