letsgotim
letsgotim

Reputation: 99

How to get week number. (I have date)?

I'm currently working on a project that needs to find the week number of a given date.

Can you give me a code snippet for my problem?

Thanks.

Upvotes: 0

Views: 3926

Answers (2)

Shishram
Shishram

Reputation: 1516

You can try this

Calendar now = Calendar.getInstance();


  now.set(Calendar.YEAR,2013);
  now.set(Calendar.MONTH,04);//0- january ..4-May
  now.set(Calendar.DATE, 04);

System.out.println("Current week of month is : " +
            now.get(Calendar.WEEK_OF_MONTH));

System.out.println("Current week of year is : " +
            now.get(Calendar.WEEK_OF_YEAR));

Upvotes: 6

Orkun Kocyigit
Orkun Kocyigit

Reputation: 1147

This should solve your problem with using this:

Calendar cal = Calendar.getInstance();
cal.setTime(date);
int weekOfYear = ca.get(Calendar.WEEK_OF_YEAR);

Upvotes: 0

Related Questions