0

How to get current week of the year using date? Below code returns current week of the year using current time. How do I change this code to take input date like 26/8/2013?(in this caseit should return 35). I want to implement by taking input date form user then return current week of the year.

Calendar calender = Calendar.getInstance();
Log.d("Current Week:", "" + calender.get(Calendar.WEEK_OF_YEAR));
Toast.makeText(this, "current year weeks is"+ calender.get(Calendar.WEEK_OF_YEAR),   
Toast.LENGTH_SHORT).show();
Caner
  • 57,267
  • 35
  • 174
  • 180
user2706686
  • 77
  • 2
  • 6
  • possible duplicate of [how to get total number of week in the current year](http://stackoverflow.com/questions/18438332/how-to-get-total-number-of-week-in-the-current-year) – Pankaj Kumar Aug 26 '13 at 08:33

1 Answers1

0
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.net.ParseException;
import android.widget.Toast;

...

/**
* @Param userInput Input date from user, format: "26/8/2013"
*/
public void getDayOfWeek(String userInput) {
    Date date;
    SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy");  
    try {  
        date = format.parse(userInput);
    } catch (ParseException e) {  
        e.printStackTrace();  
    }

    Calendar calender = new GregorianCalendar();
    calender.setTime(date);
    Toast.makeText(this, "current year weeks is" + calender.get(Calendar.WEEK_OF_YEAR), Toast.LENGTH_SHORT).show(); 
}
Caner
  • 57,267
  • 35
  • 174
  • 180
  • @NicolasTyler S/he wants to get date from user input string, so I would be happy to see how it could be done with less code. – Caner Aug 26 '13 at 07:57
  • @user2706686 I changed it a bit, try again with new code. And which line produces null pointer exception? – Caner Aug 26 '13 at 08:01
  • calender.setTime(date); date is null and also so many import options are found which which import are used? – user2706686 Aug 26 '13 at 08:05
  • ParseException give option for 3 import android.net java.text org.apache.http – user2706686 Aug 26 '13 at 08:06
  • how to make ur code dynamically?s o call any where just give input like this manner calendercurrentweek(currentfdate); and call function how tomake ur code like that?? – user2706686 Aug 26 '13 at 08:09
  • Added imports. You need to get the input from user and set `String dtStart = ...` – Caner Aug 26 '13 at 08:13
  • how to make ur function dynamically access from anywhere just give input to function like this manner calendercurrentweek(currentdate); – user2706686 Aug 26 '13 at 08:14
  • how to access ur function dynamically like this way int currentWeekno = getcurrentweeknoInYear(5/9/2013); – user2706686 Aug 26 '13 at 08:16