Skip to main content

Command Palette

Search for a command to run...

Reformat Date

Published
2 min read
Reformat Date
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 date string in the form Day Month Year, where:

  • Day is in the set {"1st", "2nd", "3rd", "4th", ..., "30th", "31st"}.

  • Month is in the set {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}.

  • Year is in the range [1900, 2100].

Convert the date string to the format YYYY-MM-DD, where:

  • YYYY denotes the 4 digit year.

  • MM denotes the 2 digit month.

  • DD denotes the 2 digit day.

LeetCode Problem - 1507

class Solution {
    public String reformatDate(String date) {
        String[] strDate = date.split(" "); // Split the input date string by space

        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 
                           "Aug", "Sep", "Oct", "Nov", "Dec"};

        int ansMonth = 1;
        // Find the month index in the months array
        for(int i=0; i<months.length; i++){
            if(strDate[1].equals(months[i])){
                ansMonth = i + 1; // Month index starts from 0, so add 1 to get the correct month number
                break;
            }
        }

        StringBuilder sb = new StringBuilder();
        // Extract the day part from the date string (removing the suffix like "th", "st", etc.)
        for(int i=0; i<strDate[0].length(); i++){
            char ch = strDate[0].charAt(i);
            if(Character.isDigit(ch)){
                sb.append(ch); // Append digits to StringBuilder
            } else {
                break; // Stop at the first non-digit character
            }
        }

        int day = Integer.parseInt(sb.toString()); // Convert StringBuilder to integer (day)

        // Format the date in ISO format "YYYY-MM-DD"
        return strDate[2] + "-" + String.format("%02d", ansMonth) + "-" + String.format("%02d", day);
    }
}

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.