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
}
Advertisements

About Vineet Verma

Developer/Blogger/Gamer/Lazy Couch Potato...:P Need PDF Books: Knowledge Base
This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s