Menu
Topics Index
...
`

Weekend Hack - 29 Jun 2013 - Results

Thanks to Pramod Jain for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Given the experience of the participants, they were really innovative and clean. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 29 Jun 2013 - 1 is Pramod Jain. He will get a recharge or gift voucher worth Rs. 125.

To create the advanced time String for the distance between two dates

Write a program to create the advanced time String for the distance between two dates. In the string Y represents Years, L represents Leap Year, M for Months, W for Weeks, S represents Sunday or Saturday, M represents Monday, T represents Tuesday or Thursday, W represents Wednesday, F represents Friday.
Note : If the start date is greater than end date return -1. If both dates are same, then return empty string.

Input (String, String) Output (String)
01-Jan-2000, 09-Dec-2003 LYYMMMMMMMMMMMWT
28-Jan-2000, 01-Mar-2000 MTW
15-Aug-2001, 15-July-2002 MMMMMMMMMMM
07-Feb-2002, 01-Jan-2012 YYLYYYLYYMMMMMMMMMMWWWTFSS
05-Sept-2009, 08-Oct-2008 -1
28-Jan-1995, 14-Oct-1997 YLMMMMMMMMWWMT

Solution 1 - Submitted on Sat Jun 29 22:23:43 2013

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

class CreateAdvancedTimeString {

    public static void main(String s[]) {
        String startDate = "12-Feb-1998", endDate = "01-Mar-2000";
        String timeString = createAdvancedTimeString(startDate, endDate);
        System.out.println("The Time String is " + timeString);
    }

    public static String createAdvancedTimeString(String first, String last) {
        String timeGap = new String();
        Calendar startDate = new GregorianCalendar();
        Calendar endDate = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        //converting strings to calendar objects
        try {
            startDate.setTime(sdf.parse(first));
            endDate.setTime(sdf.parse(last));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //If the startDate is greater than endDate then returning -1.
        if (startDate.after(endDate))
            return "-1";
        // Loop for adding one year to startDate object (continuing the loop until startDate exceeds endDate)
        while (!startDate.after(endDate)) {
            startDate.add(Calendar.YEAR, 1);
            int year;
            // If the month is january-0 or february-1 then check the present year whether leap or not
            if (startDate.get(Calendar.MONTH) < 2)
                year = startDate.get(Calendar.YEAR) - 1;
            // else check for the next year whether leap or not
            else
                year = startDate.get(Calendar.YEAR);
            // checking for leap year
            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
                timeGap = timeGap.concat("L");
            else
                timeGap = timeGap.concat("Y");
        }
        // As the startDate has exceeded endDate moving startDate one year back.
        startDate.add(Calendar.YEAR, -1);
        // Removing the last character of the string timeGap (for the removal of last year)
        timeGap = timeGap.substring(0, timeGap.length() - 1);
        // Loop for adding one month to startDate object (continuing the loop until startDate exceeds endDate)
        while (!startDate.after(endDate)) {
            startDate.add(Calendar.MONTH, 1);
            timeGap = timeGap.concat("M");
        }
        // As the startDate has exceeded endDate moving startDate one month back.
        startDate.add(Calendar.MONTH, -1);
        // Removing the last character of the string timeGap (for the removal of last month)
        timeGap = timeGap.substring(0, timeGap.length() - 1);
        // Loop for adding one week to startDate object (continuing the loop until startDate exceeds endDate)
        while (!startDate.after(endDate)) {
            startDate.add(Calendar.WEEK_OF_YEAR, 1);
            timeGap = timeGap.concat("W");
        }
        // As the startDate has exceeded endDate moving startDate one week back.
        startDate.add(Calendar.WEEK_OF_YEAR, -1);
        // Removing the last character of the string timeGap (for the removal of last week)
        timeGap = timeGap.substring(0, timeGap.length() - 1);
        // Loop for adding one day to startDate object (continuing the loop until startDate equals endDate)
        while (startDate.before(endDate)) {
            startDate.add(Calendar.DAY_OF_YEAR, 1);
            switch (startDate.get(Calendar.DAY_OF_WEEK)) {
                case 1 :
                case 7 :
                    timeGap = timeGap.concat("S"); // S for Sunday and Saturday
                    break;
                case 2 :
                    timeGap = timeGap.concat("M"); // M for Monday
                    break;
                case 3 :
                case 5 :
                    timeGap = timeGap.concat("T"); //T for Tuesday
                    break;
                case 4 :
                    timeGap = timeGap.concat("W"); //W for Wednesday
                    break;
                case 6 :
                    timeGap = timeGap.concat("F"); //F for Friday 
                    break;
            }
        }
        return timeGap;
    }
}

Comments :

© meritcampus 2019

All Rights Reserved.

Open In App