i want to have FirstDay of week and last Day of week into the Date Format means i need whole Week date in java using DateChooser means i need to have that whole Existing week for that date. show how to get it i trying to have that logic. Please Help me and thank you in advance for your Response..
Asked
Active
Viewed 1,826 times
2
-
i just need to use First day of week Date and last day of week Date in program. for ex if i select one date form DatePicker then it must show that whole week of that date. now is it clear ?? :) – Kishan Bheemajiyani Nov 15 '13 at 06:50
2 Answers
3
You can use the Calendar class.
Calendar cal = Calendar.getInstance ();
int dayofWeek = cal.get (Calendar.DAY_OF_WEEK);
cal.add (Calendar.DATE, -1 * (dayofWeek - Calendar.MONDAY));
Date fdow = cal.getTime ();
As the cal object is now modified, just add six
to get the last day of the week.
cal.add (Calendar.DATE, 6);
Date ldow = cal.getTime ();

Krease
- 15,805
- 8
- 54
- 86

Scary Wombat
- 44,617
- 6
- 35
- 64
-
1that will add One week. but i need that last date of that week too. – Kishan Bheemajiyani Nov 15 '13 at 07:03
-
I am using Calender Class but i just need To have First day of week and last day of week in Form of date i please can u modify code with that clearly. :) – Kishan Bheemajiyani Nov 15 '13 at 07:04
-
I am multiplying by `-1` so that will subtract. For the last day of the week add until Sunday. – Scary Wombat Nov 15 '13 at 07:08
-
1
-
-
Your code have Error. that u have set date in VOID return type.. – Kishan Bheemajiyani Nov 15 '13 at 07:18
-
-
Still its haveing Some Spelling mistake means Spelling Errors let me correct it. :) – Kishan Bheemajiyani Nov 15 '13 at 07:30
-
-
But it may be having Exception when you Change Date. means changing Month its showing Same Data. – Kishan Bheemajiyani Nov 15 '13 at 09:37
-
Still its Showing Same year when i even change Year for this. so just look once i am going to change code and will write Correct it i need your help too. – Kishan Bheemajiyani Nov 15 '13 at 10:09
1
Using the third-party Joda-Time makes it a bit easier.
See the question Get first day of a particular week in Joda-Time? java.

Community
- 1
- 1

Basil Bourque
- 303,325
- 100
- 852
- 1,154
-
but i need to Set that date form the Jcalander i can not set that date using Local Date. – Kishan Bheemajiyani Nov 15 '13 at 10:07
-
@Krishna You can do most of your work in Joda-Time objects and then at the end convert to a [java.util.Date](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html) object. Be aware of time zone, otherwise very simple to do. See the question, [How to convert Joda-Time to Date of java.util.Date and vice versa?](http://stackoverflow.com/questions/15333320/how-to-convert-joda-time-to-date-of-java-util-date-and-vice-versa) – Basil Bourque Nov 15 '13 at 18:23
-
ya you are right. but if u dont mind can i ask you for one favor?? can u just paste a sample code in answer so i can have better idea. – Kishan Bheemajiyani Nov 16 '13 at 04:41
-
@Krishna I already gave you links to two other questions whose answers include such source code examples. Also, you have not yet cleaned up your question for clarity as requested by the moderators. So how can I write example code when the question is not yet clear? – Basil Bourque Nov 16 '13 at 04:58
-
just do one thing take 2 Joda Date and take one particuler date form that and find out that particular week date and show firstday of week date and lastday date of that week. and convert it to java.util Date. – Kishan Bheemajiyani Nov 16 '13 at 08:35