Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all week dates for given date java

Tags:

java

date

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.

like image 364
Joe Avatar asked Jul 17 '15 02:07

Joe


People also ask

How do you get the day of the week from a date in Java?

To get the day of week for a particular date in Java, use the Calendar. DAY_OF_WEEK constant.

How can I get a list of dates between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

How do you sum dates in Java?

getTime(); Date sumDate = new Date(sum); The code uses the . getTime() method that returns the number of milliseconds since the epoch.


1 Answers

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
like image 91
akash Avatar answered Nov 05 '22 14:11

akash