
Calendar c = CalendargetInstance();
Systemoutprintln(cgetTime());
cset(CalendarYEAR, 2014);
cset(CalendarMONTH, CalendarDECEMBER);
cset(CalendarDATE, 29);
Systemoutprintln(cgetTime());
cset(CalendarDATE,35);
Systemoutprintln(cgetTime());
是这个意思吗?
你获取本周周日的日期,得到的就是本周日和下一周一到周六的日期段,你再用Calendar把本周日的日期加上七天,就得到下一周周日的日期,这样就全了。
这只是我的思路,你最好把代码贴出来,大家帮你分析一下,也好解决问题。
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星期日
Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。下面的例子供参考。更详细的内容(比如yyyy,MM,dd各代表什么)可以参考javadoc。
public class DateUtil
{
private static final SimpleDateFormat dateFormat = null;
static
{
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
// 设置lenient为false 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
dateFormatsetLenient(false);
}
public static boolean isValidDate(String s)
{
try
{
dateFormatparse(s);
return true;
}
catch (Exception e)
{
// 如果throw javatextParseException或者NullPointerException,就说明格式不对
return false;
}
}
// 下面这个方法则可以将一个日期按照你指定的格式输出
public static String formatDate(Date d)
{
return dateFormatformat(d);
}
}
以上就是关于利用java计算上上周,上周,本周,下周的起止时间(可实现跨年,跨月)全部的内容,包括:利用java计算上上周,上周,本周,下周的起止时间(可实现跨年,跨月)、java 日期 下一周的周一 周日 分别是多少号、java 日期,根据今天的日期,输出这一周的其他日期,例如:2013-7-31,输出这周从第一天开始到今天的日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)