Week of Day String to INT

To change a Day of the week like Monday, Tuesday and so on to integer the use the following code:

  public int dayNameComparison(String dayName) {
           java.text.DateFormatSymbols objDaySymbol = new java.text.DateFormatSymbols();
           String symbolDayNames[] = objDaySymbol.getWeekdays();
           for (int countDayname = 0; countDayname < symbolDayNames.length;countDayname++) {
                if (dayName.equalsIgnoreCase(symbolDayNames[countDayname])) {
                      return countDayname;
                   }
               }
             return 0;
          }

 

This way when u pass the day as parameter the you will get the output as integer. This output can be used directly in Calendar class as input :

 

public static void main(String[] args) {
         TestMouse testMouse = new TestMouse();
         int i = testMouse.dayNameComparison("Tuesday");
         System.out.println("" + i);
         java.util.Calendar calendar = java.util.Calendar.getInstance();
         calendar.set(java.util.Calendar.DAY_OF_WEEK, i);
         System.out.println(new java.text.SimpleDateFormat("EEEE").format(calendar.getTime()));//Prints: Tuesday
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.