android: Convert any date into week number

I'd like to find out the week number of the year from any given date.

I've tried this:

int date = 17.08.2015 or date = 17082015
Calendar calendar = Calendar.getInstance(); 
date = calendar.get(Calendar.WEEK_OF_YEAR);

I thought it would work, but it did not.

Background:

I have a DatepickerDialog where the user can select the date. But I just need the weeknumber.

Thanks in advance

Answers


You can get week number using this method.

public int getDayOfWeek(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_WEEK);
}

you have to pass formatted date to this method and it returns week number.

EDIT1:

SimpleDateFormat format = new SimpleDateFormat("EEEE", Locale.getDefault());;
Date date;
Calendar calendar = Calendar.getInstance();
try {
       date = format.parse(format.format(calendar.getTime()));
       int weekDay = getDayOfWeek(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }

EDIT2:

String dtStart = "2010-10-15";  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
try {  
    Date date = format.parse(dtStart);  
    //this date can be passed to getDayOfWeek method
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

I hope it helps!


from google search i have found this link.please check this once you can get the right solution


It wont work like that. You need to parse the date first. Have a look at the following code.

String strDate = "17.08.2015";
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = null
try{
    date = dateFormat.parse(strDate)
}
catch(Exception e){
    e.printStackTrace();
}

Calendar now = Calendar.getInstance();
now.setTime(date);
int week = now.get(Calendar.WEEK_OF_YEAR);

So when you select a date from Calendar (DatePicker). Just format that date in a particular date format and parse it and set it in Calendar. Get the week number like using above code.


try this one...

   SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
  Calendar calendar=Calendar.getInstance();
  String demoDate="2015-01-15 11:09:00";
        try{
            calendar.setTime(dateFormat.parse(demoDate));
            int weekOfYear=Calendar.WEEK_OF_YEAR);
        }catch(Exception e){
            e.printStackTrace();
        }  

for more info try this link for simple date format: http://developer.android.com/reference/java/text/SimpleDateFormat.html


Need Your Help

How to fix the uninitialized constant Rake::DSL problem on Heroku?

ruby-on-rails ruby-on-rails-3 heroku rake

I am getting errors similar to the ones in these questions, except mine are occuring on Heroku:

Exporting in Netsuite

export-to-csv netsuite

One of our customer's uses Netsuite ERP. We would like to be able accept pick tickets(orders) from them in a format that would allow us to import the pick tickets into our custom built ERP system.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.