0

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

JGPhilip
  • 1,388
  • 17
  • 19
Radi
  • 15
  • 5
  • I'm confused… Your question is all about *week-of-year* (1-53), but you accepted an answer about *day-of-week* (Monday, Tuesday, …). – Basil Bourque Aug 17 '15 at 21:12

5 Answers5

0

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.

Noman Rafique
  • 3,735
  • 26
  • 29
0

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

Vishwa
  • 1,112
  • 1
  • 11
  • 23
0

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!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • I am a little confused.. sorry. But where do i asign my variable ? I don´t get it with Date date; I´d like to use my own custom date. – Radi Aug 17 '15 at 13:31
  • I dont understand. Why you formated the date and again parsed it here `date = format.parse(format.format(calendar.getTime()));`. Why not simply assign `date = calendar.getTime();`. – Noman Rafique Aug 17 '15 at 13:50
  • Because calendar.getTime() will return different date format. you have to convert to date format you want. calendar.getTime() will return current time if we have not set any date to calender instance. – Rajesh Jadav Aug 17 '15 at 13:52
  • No! Thats wrong. You can print both times, you'll get the same result. Date will be same in `date` variable in both cases. You are formatting and parsing just like that and there is no need to do that. – Noman Rafique Aug 17 '15 at 14:01
  • I have put my code which i used according to my purpose. so in your case there is no need for it. – Rajesh Jadav Aug 17 '15 at 14:03
0

If you want you can also try this so you can get your current date as variable

 DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
 String date_posted = df.format(Calendar.getInstance().getTime());

    Date date = null;

    try {

        date = df.parse(date_posted);

    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar now = Calendar.getInstance();
    now.setTime(date);

    int week = now.get(Calendar.WEEK_OF_YEAR);
Cyd
  • 1,245
  • 1
  • 14
  • 17
-1

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

Community
  • 1
  • 1
RamBabu Pudari
  • 912
  • 7
  • 19
  • Thanks for participating in StackOverflow. Please expand your answer. Link-only does no make for a good Answer on StacKOverflow. Please summarize the link’s information here. – Basil Bourque Aug 17 '15 at 16:57
  • Atleast give an answer and refer to the link – Cyd Jan 07 '20 at 11:46