我希望从给定日期的周一到周五获得周日。
假设给定日期是6-2-2013
我想要结果:
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
或者甚至我将周数作为6 ...我如何实现关于结果
我从下面的代码
获得了周数Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 6);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
答案 0 :(得分:2)
试试这个...我已经编辑并添加了更多更改Achintya Jha Code ...工作正常 感谢Achintya Jha
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 14);
c.set(Calendar.MONTH, 1);
c.set(Calendar.YEAR, 2013);
int weekNo = c.get(Calendar.WEEK_OF_YEAR);
//Set the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.clear();
//Set the Week Year and the Week No
c.set(Calendar.WEEK_OF_YEAR, weekNo);
c.set(Calendar.YEAR, 2013);
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd/MM/yyyy"); // PST`
//Get Week Start Date
Date startDate = c.getTime();
//Now it will be sunday so add plus one so now it becomes Monday
c.add(Calendar.DATE, 1);
for (int i = 0; i < 5; i++) {
//From Monday to Friday Dates will be Printed
System.out.println(formatter.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
答案 1 :(得分:1)
2013年2月2日:
GregorianCalendar c = new GregorianCalendar(2013, 1, 6);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
prints
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
from week 6 of 2013:
GregorianCalendar c = new GregorianCalendar(2013, 0, 1);
c.add(Calendar.WEEK_OF_YEAR, 6 - 1);
c.add(Calendar.DAY_OF_YEAR, Calendar.MONDAY - c.get(Calendar.DAY_OF_WEEK));
DateFormat df = new SimpleDateFormat("d-M-yyyy");
while(c.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
if (c.get(Calendar.YEAR) == 2013) {
System.out.printf("%-10tA%10s%n", c.getTime(), df.format(c.getTime()));
}
c.add(Calendar.DAY_OF_YEAR, 1);
}
打印
Monday 4-2-2013
Tuesday 5-2-2013
Wednesday 6-2-2013
Thursday 7-2-2013
Friday 8-2-2013
答案 2 :(得分:0)
public static void main(String[] args) {
getWeekOfDates(2013, 2, 11);
}
private static void getWeekOfDates(int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
c.set(Calendar.WEEK_OF_MONTH, weekOfMonth);
DateFormat df = new SimpleDateFormat("EEE dd-MM-yyyy");
for (int i = 0; i < 5; i++) {
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE, 1);
}
}
试试这个:我根据您的要求进行了编辑。
输出:
Mon 11-03-2013
Tue 12-03-2013
Wed 13-03-2013
Thu 14-03-2013
Fri 15-03-2013