Get all days of the current week?

I need to get the number of all days of the week (from MON to SUN) based on which day is sent as a parameter.

For instance,
I pass on May 2 and I get an array [30,1,2,3,4,5,6], which is actually this week.
Or I turn in on May 16 and I get an array [14,15,16,17,18,19,20].

I tried to use this code, but it returns after 7 days from today, which I do not want.

//get which week of month Calendar now = Calendar.getInstance(); now.set(mYear, mMonth, Integer.parseInt(extractedDay)); int weekOfMonth = now.get(Calendar.WEEK_OF_MONTH); //get all days of clicked week SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.set(mYear, mMonth, Integer.parseInt(extractedDay)); String[] days = new String[7]; for (int i = 0; i < 7; i++) { days[i] = format.format(calendar.getTime()); calendar.add(Calendar.DAY_OF_MONTH, 1); } 

But that is not what I need. I need to go today day (THU) and get the dates of this week of May - Monday, April 30 to the Sun, May 6.

EDIT

I realized that I have a code that determines which week of the month. Is there any way to use this data and set the date for Monday? For example, I walk through May 16th and then discover that this is the 3rd week of May, and I set the variable firstDayOfWeek (new variable) until May 14th.

+6
source share
4 answers

First you must subtract the weekdays of the day you choose to start on the first day of the week. Try the following code:

 public static void main(String[] args){ Calendar now = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); String[] days = new String[7]; int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 2; //add 2 if your week start on monday now.add(Calendar.DAY_OF_MONTH, delta ); for (int i = 0; i < 7; i++) { days[i] = format.format(now.getTime()); now.add(Calendar.DAY_OF_MONTH, 1); } System.out.println(Arrays.toString(days)); } 

Today he outputs:

[04/30/2012, 05/01/2012, 05/02/2012, 05/03/2012, 05/04/2012, 05/05/2012, 05/06/2012]

+19
source

Your question has already been answered, but I would like to add a little. Your code is problematic in two ways:

  • It uses various data types for the start date ( mYear is int and exactDay is String ?).
  • It mixes parsing, formatting, and generating results.

This makes it difficult for you to verify the code!

Therefore, I propose a different approach:

 public class DaysOfCurrentWeek { public static void main(String[] args) { Date refDate = new Date(); Date[] days = getDaysOfWeek(refDate, Calendar.getInstance().getFirstDayOfWeek()); for (Date day : days) { System.out.println(day); } } private static Date[] getDaysOfWeek(Date refDate, int firstDayOfWeek) { Calendar calendar = Calendar.getInstance(); calendar.setTime(refDate); calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek); Date[] daysOfWeek = new Date[7]; for (int i = 0; i < 7; i++) { daysOfWeek[i] = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); } return daysOfWeek; } } 

The code works as follows:

  • Create a key date (in this example, only with new Date() ).
  • Call getDaysOfWeek() to do the dirty work. After driving firstDayOfWeek , you can choose MONDAY or SUNDAY as the beginning of the week. In this example, I just use the default settings for the calendar.
  • Return the result - unformatted and like Date . Thus, you can let the calling code decide what to do - print it or do further calculations.

Now you can simply test the getDaysOfWeek () method without worrying about formatting or how the reference date was created.

+4
source

You will need to use this line of code somewhere in your current code:

 calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 

it should be at least before the for loop.

+1
source

you need to set the day after to set the date

calendar.set(mYear, mMonth, Integer.parseInt(extractedDay)); calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

0
source

Source: https://habr.com/ru/post/914795/


All Articles