Get the day of the week from a string to a Calendar value - Android java.util

Android examples for java.util:Week

Description

Get the day of the week from a string to a Calendar value

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) {
        String dayOfWeek = "java2s.com";
        System.out.println(convertToDayOfWeekValue(dayOfWeek));
    }/*from w w w . j  a v a  2  s .c  o  m*/

    /** Get the day of the week from a string to a Calendar value
     * @param dayOfWeek The day of the week in a string format.
     * @return The Calendar value
     */
    public static int convertToDayOfWeekValue(String dayOfWeek) {
        // Some days have a single letter abbreviation, some have two. Start with trying to find two day
        // abbreviations. 
        // Get the first two letters of the week.
        String shortDayOfWeek;
        if (dayOfWeek.length() > 1) {
            shortDayOfWeek = dayOfWeek.toLowerCase().substring(0, 2);

            if (shortDayOfWeek.equals("th"))
                return Calendar.THURSDAY;
            else if (shortDayOfWeek.equals("sa"))
                return Calendar.SATURDAY;
            else if (shortDayOfWeek.equals("su"))
                return Calendar.SUNDAY;
        }

        // Now try the single letter of week.
        shortDayOfWeek = dayOfWeek.toLowerCase().substring(0, 1);

        if (shortDayOfWeek.equals("m"))
            return Calendar.MONDAY;
        else if (shortDayOfWeek.equals("t"))
            return Calendar.TUESDAY;
        else if (shortDayOfWeek.equals("w"))
            return Calendar.WEDNESDAY;
        else if (shortDayOfWeek.equals("f"))
            return Calendar.FRIDAY;
        else
            return -1;

    }
}

Related Tutorials