• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 736
  • Last Modified:

how to Caluclate This Week and Last week start and End Dates ?

I have four radio buttons for Today | This Week | Last Week  | Date

i can easily figure out Date and Today.

I need help with "This Week" And "Last week "

Could you please tell me how should i cacluate start and end dates of "This Week" and "last week"  in Java

i know i ahve to use calender. I need code please :(
0
Siddhardh
Asked:
Siddhardh
  • 3
  • 2
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
Here is a rough example of how you would do this...
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_WEEK, 1-cal.get(Calendar.DAY_OF_WEEK));
Date startOfWeek = cal.getTime();
 
cal.add(Calendar.DAY_OF_WEEK, 6);
Date endOfWeek = cal.getTime();
 
cal.add(Calendar.DAY_OF_WEEK, -7);
Date endOfLastWeek = cal.getTime();
 
cal.add(Calendar.DAY_OF_WEEK, -6);
Date startOfLastWeek = cal.getTime();
		
System.out.println("Start of this week is: " + startOfWeek.toString());
System.out.println("End of this week is: " + endOfWeek.toString());
System.out.println("Start of last week is: " + startOfLastWeek.toString());
System.out.println("End of last week is: " + endOfLastWeek.toString());		

Open in new window

0
 
SiddhardhAuthor Commented:
Thanks a lot !
0
 
SiddhardhAuthor Commented:
I closed it without verifying sorry.The solution you gave takes Sunday as Start Date and Saturday as End Date. I want Monday as Start Day in the Week and Sunday as End Day in the week. I need their dates.
0
 
SiddhardhAuthor Commented:
I did this got it

cal.add(Calendar.DAY_OF_WEEK, 2-cal.get(Calendar.DAY_OF_WEEK));
            Date startOfWeek = cal.getTime();
             
            cal.add(Calendar.DAY_OF_WEEK, 6);
            Date endOfWeek = cal.getTime();
             
            cal.add(Calendar.DAY_OF_WEEK, -7);
            Date endOfLastWeek = cal.getTime();
             
            cal.add(Calendar.DAY_OF_WEEK, -6);
            Date startOfLastWeek = cal.getTime();
0
 
Kevin CrossChief Technology OfficerCommented:
That works, glad you got it.


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now