DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Handling Weeks in Java 7

Handling Weeks in Java 7

Learn a handy trick to incorporate and output the number of weeks in the year in your code.

Jasdhir Singh user avatar by
Jasdhir Singh
·
Nov. 24, 16 · Tutorial
Like (4)
Save
Tweet
Share
8.83K Views

Join the DZone community and get the full member experience.

Join For Free

Some applications, in their business logic, deal with the number of weeks in a year and the current week of the year. There are 52 weeks in a year, but 52 weeks multiplied by 7 days per week equals 364 days per year, not the actual 365 days. A week number is used to refer to the week of the year. 

Java 7 has introduced several methods to support determining the week of the year.

Some implementations of the abstract java.util.Calendar class do not support week calculations. To determine if the calendar implementation supports week calculations, we need to execute the isWeekDateSupported method. It returns true if the support is provided. To return the number of weeks for the current calendar year, use the getWeeksInWeekYear method. To determine the week for the current date, use the get method with the WEEK_OF_YEAR as its argument.

Below is the code to get the total number of weeks and current week.

import java.util.Calendar; 
public class WeeksInYear {     
    public static void main(String[] args) {  
        Calendar calendar = Calendar.getInstance();             
        if(calendar.isWeekDateSupported()) {               
            System.out.println("Number of weeks in this year: " + calendar.getWeeksInWeekYear());                   
            System.out.println("Current week number: " + calendar.                               get(Calendar.WEEK_OF_YEAR));         
        }     
    }
}


Number of weeks in this year: 53. The above code will give the following output

Current week number: 47

Explanation of Code

An instance of the Calendar class is created, which is an instance of the GregorianCalendar class. An if statement was controlled by the isWeekDateSupported method. It returned true, which resulted in the execution of the getWeeksInWeekYear and get methods. The get method was passed in the field WEEK_OF_YEAR, which returned the current week number.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Set Up and Run Cypress Test Cases in CI/CD TeamCity
  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java
  • Integrate AWS Secrets Manager in Spring Boot Application
  • File Uploads for the Web (2): Upload Files With JavaScript

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: