Get the start and end date of the week from the week and year number in android

I want to get the start date and end date of the week, for the week number passed to the method. For example, if I pass the week number as 51 and the year as 2011 , it should return me a start date as 18 Dec 2011 and an end date as 24 Dec 2011

Are there any methods to help me achieve this?

+7
source share
2 answers

You can use the following method to get the first date and end date of the week.

  void getStartEndOFWeek(int enterWeek, int enterYear){ //enterWeek is week number //enterYear is year Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(Calendar.WEEK_OF_YEAR, enterWeek); calendar.set(Calendar.YEAR, enterYear); SimpleDateFormat formatter = new SimpleDateFormat("ddMMM yyyy"); // PST` Date startDate = calendar.getTime(); String startDateInStr = formatter.format(startDate); System.out.println("...date..."+startDateInStr); calendar.add(Calendar.DATE, 6); Date enddate = calendar.getTime(); String endDaString = formatter.format(enddate); System.out.println("...date..."+endDaString); } 
+19
source

You need to use the java.util.Calendar class. You can set the year using Calendar.YEAR and the week of the year using Calendar.WEEK_OF_YEAR using the public void set(int field, int value) method.

If the language is set correctly, you can use setFirstDayOfWeek to change the first day of the week. The date indicated by your calendar instance will be your start date. Just add 6 days for your end date.

 Calendar calendar = new GregorianCalendar(); // Clear the calendar since the default is the current time calendar.clear(); // Directly set year and week of year calendar.set(Calendar.YEAR, 2011); calendar.set(Calendar.WEEK_OF_YEAR, 51); // Start date for the week Date startDate = calendar.getTime(); // Add 6 days to reach the last day of the current week calendar.add(Calendar.DAY_OF_YEAR, 6); // End date for the week Date endDate = calendar.getTime(); 
+3
source

All Articles