如何获取给定日期java的所有周日期 [英] How to get all week dates for given date java

查看:563
本文介绍了如何获取给定日期java的所有周日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期,如何让所有的日期落在给定日期属于java的星期?

 例如:

如果我给今天的日期,那么我应该得到属于本周的所有日期。

2015年7月12日至2015年7月18日

可以帮助我



我试图获得给定日期的周日期,我已经给出了解释为什么这个问题不重复,请在评论前阅读。

解决方案

您可以尝试以下方式

  //cal.setTime(new Date()); //如果要

设置特定日期(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i ++){
cal.set(Calendar.DAY_OF_WEEK,i);
System.out.println(cal.getTime()); //返回日期
}

OUTPUT

  Sun Jul 12 08:12 :38 IST 2015 
Mon Jul 13 08:12:38 IST 2015
Tue Jul 14 08:12:38 IST 2015
Wed Jul 15 08:12:38 IST 2015
Thu Jul 16 08:12:38 IST 2015
Fri Jul 17 08:12:38 IST 2015
Sat Jul 18 08:12:38 IST 2015


i have a date and how to get all the dates fall on the week that the given date belongs in java?

example:

if i give today's date then i should get all dates belonging to this week.

12 July 2015 to 18 July 2015

could some one help me this please.

i am trying to get the week dates for the given date, I have given explanation why this question is not duplicate, Please read before commenting.

解决方案

You can try following way,

Calendar cal = Calendar.getInstance();
//cal.setTime(new Date());//Set specific Date if you want to

for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++) {
    cal.set(Calendar.DAY_OF_WEEK, i);
    System.out.println(cal.getTime());//Returns Date
}

OUTPUT

Sun Jul 12 08:12:38 IST 2015
Mon Jul 13 08:12:38 IST 2015
Tue Jul 14 08:12:38 IST 2015
Wed Jul 15 08:12:38 IST 2015
Thu Jul 16 08:12:38 IST 2015
Fri Jul 17 08:12:38 IST 2015
Sat Jul 18 08:12:38 IST 2015

这篇关于如何获取给定日期java的所有周日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆