week-number.net

Programming Languages

Week number in Java

To get the week number for a date in Java, use the Calendar class. It has a constant called WEEK_OF_YEAR to get the calendar week number for a specific date.

Example

Get the current week number and print it.


Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();   
calendar.setTime(trialTime);     
System.out.println("Week number:" + 
    calendar.get(Calendar.WEEK_OF_YEAR)); 

Output:

Week number:38
comments powered by Disqus