java 获取下一周的日期,使用java日历类获取一周的开始和结束日期

这篇博客介绍了如何使用Java Calendar类获取指定日期(如2011年10月12日)的一周的起始(10月10日)和结束(10月16日)日期。通过clone和add方法实现,并使用joda-time库解决日期问题。
摘要由CSDN通过智能技术生成

I want to get the last and the first week of a week for a given date.

e.g if the date is 12th October 2011 then I need the dates 10th October 2011 (as the starting date of the week) and 16th october 2011 (as the end date of the week)

Does anyone know how to get these 2 dates using the calender class (java.util.Calendar)

thanks a lot!

解决方案

Some code how to do it with the Calendar object. I should also mention joda time library as it can help you many of Date/Calendar problems.

Code

public static void main(String[] args) {

// set the date

Calendar cal = Calendar.getInstance();

cal.set(2011, 10 - 1, 12);

// "calculate" the start date of the week

Calendar first = (Calendar) cal.clone();

first.add(Calendar.DAY_OF_WEEK,

first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));

// and add six days to the end date

Calendar last = (Calendar) first.clone();

last.add(Calendar.DAY_OF_YEAR, 6);

// print the result

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

System.out.println(df.format(first.getTime()) + " -> " +

df.format(last.getTime()));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值