Friday, November 19, 2010

Java Code: How to find out the weeknumber in a month to which a given date belong to?

The below code will provide the week number in the format "Week -" + Weeknumber.

    public static String getWeekForDate(Date currentDate) {
        String weekNumber = "";
        ArrayList<Date> weeksList = new ArrayList<Date>();
        int dateCounter = 1;
        int month = currentDate.getMonth();
        Date week1Start = new Date(currentDate.getYear(), month, dateCounter);
        weeksList.add(week1Start);
        Date nextWeekStart = null;
        boolean cont = true;
        while (cont) {
            dateCounter++;
            Date nextDay = new Date(currentDate.getYear(), currentDate
                    .getMonth(), dateCounter);
            if (0 == nextDay.getDay()) {
                nextWeekStart = new Date(currentDate.getYear(), currentDate
                        .getMonth(), dateCounter);
                int tempMonth = nextWeekStart.getMonth();
                if (tempMonth != month) {
                    Date monthEnd = new Date(currentDate.getYear(), currentDate
                            .getMonth() + 1, 1);
                    weeksList.add(monthEnd);
                    cont = false;
                } else {
                    weeksList.add(nextWeekStart);
                }
            }
        }
        for (int index = 0; index < weeksList.size() - 1; index++) {
            if (currentDate.getTime() >= ((Date) weeksList.get(index))
                    .getTime()
                    && currentDate.getTime() < ((Date) weeksList.get(index + 1))
                            .getTime())
                weekNumber = "Week " + (index + 1);
        }
        return weekNumber;
    }

Hope this helps

Java Script : How to bookmark through Javascript function

The below JS function bookmarks a link. All that you need to do is to invoke the javascript function when the user clicks on Bookmark button.

function bookmark()
{
    bookmarkurl="http://www.google.com"
    bookmarktitle="Google"
    if (document.all)
        window.external.AddFavorite(bookmarkurl,bookmarktitle)
    else if (window.sidebar) // firefox
        window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
}

Java Code: How to map a date with a quarter

This simple code will give you the quarter to which the provided date belong to. We need to provide the FY Start date too.

    public String getQuarter(Date date){
       
        int monthNo = date.getMonth();
        int year = date.getYear();
        int temp = year - 100;
        String quarterInfo = null;
        int fyStart = getFyStart(); //4 if fyStarts in April
        if (monthNo < fyStart) {
            monthNo = 12 + monthNo;
        } else {
            temp = temp + 1;
        }

        int q1End = fyStart + 3;
        int q2End = q1End + 3;
        int q3End = q2End + 3;
        int q4End = q3End + 3;

        if (monthNo >= fyStart && monthNo < q1End) {
            quarterInfo = "Q1" + " \'" + temp;
        } else if (monthNo >= q1End && monthNo < q2End) {
            quarterInfo = "Q2" + " \'" + temp;
        } else if (monthNo >= q2End && monthNo < q3End) {
            quarterInfo = "Q3" + " \'" + temp;
        } else if (monthNo >= q3End && monthNo < q4End) {
            quarterInfo = "Q4" + " \'" + temp;
        } else {
            quarterInfo = "None";
        }
        return quarterInfo;
    }

Hope this helps

CVS - How to Merge branch code to Head in CVS

Follow the below steps to merge your changes:
  1. Import the code from Head into Eclipse workspace
  2. Select the project and choose the option Team > Merge.
  3. Select the name of the Branch from which code needs to be merged.
  4. Click Finish.
  5. A CVS Merge synchronization will be added to the Synchronize view, showing all differences between your workspace and the branch with the changes.
  6. choose Update, Override and Update, or Mark as Merged.
  7. After all desired changes are in the workspace, choose Team > Synchronize with Repository
  8. You may then commit all the changes to the repository.
Hope this helps