2

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..

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
  • 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 Answers2

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
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