Skip to main content

Command Palette

Search for a command to run...

Day of the Year

Published
1 min read
Day of the Year
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

LeetCode Problem - 1154

import java.time.Year;

class Solution {

    // Method to calculate the day of the year for a given date in the format "yyyy-mm-dd"
    public int dayOfYear(String date) {
        // Extract the year, month, and day from the date string
        int year = Integer.valueOf(date.substring(0, 4)); 
        int month = Integer.valueOf(date.substring(5, 7)); 
        int day = Integer.valueOf(date.substring(8, 10));

        // Determine if the year is a leap year and set February days accordingly
        int feb = 28;
        if (Year.of(year).isLeap()) feb = 29;

        // Array holding the number of days in each month
        int[] months = {31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        // Initialize total days of the year with the given day
        int totalDaysOfYear = day;

        // Add the days of the previous months to the total
        for (int i = month - 2; i >= 0; i--) {
            totalDaysOfYear += months[i];
        }

        // Return the total number of days since the start of the year
        return totalDaysOfYear;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.