Create a Gregorian Calendar with a year, month string, and a day of month. - Android java.util

Android examples for java.util:Month

Description

Create a Gregorian Calendar with a year, month string, and a day of month.

Demo Code


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

public class Main{
    public static void main(String[] argv){
        int year = 2;
        String monthString = "java2s.com";
        int dayOfMonth = 2;
        System.out.println(createGregorianCalendar(year,monthString,dayOfMonth));
    }//  w  w  w .j  a  v a  2  s  .c om
    /**
     * Create a Gregorian Calendar with a year, month string, and a day of month.
     * @param year The year of the Calendar
     * @param monthString The month string of the Calendar
     * @param dayOfMonth The day of the month of the Calendar
     * @return The new Gregorian Calendar
     */
    public static GregorianCalendar createGregorianCalendar(int year,
            String monthString, int dayOfMonth) {
        GregorianCalendar calendar = new GregorianCalendar(year,
                CalendarUtils.convertToMonthValue(monthString), dayOfMonth);
        return calendar;
    }
    /**
     * Create a Gregorian Calendar with a year, month string, and a day of month.
     * @param year The year of the Calendar
     * @param monthString The month string of the Calendar
     * @param dayOfMonth The day of the month of the Calendar
     * @return The new Gregorian Calendar
     */
    public static GregorianCalendar createGregorianCalendar(
            String yearString, String monthString, String dayOfMonthString) {
        return createGregorianCalendar(Integer.parseInt(yearString),
                monthString, Integer.parseInt(dayOfMonthString));
    }
    /**
     * Get the Java Gregorian Calendar value based on the matching month string. 
     * @param month The month of the calendar. Only the first three letters are compared.
     * @return The Java value of the Gregorian Calendar.
     */
    public static int convertToMonthValue(String month) {
        // Get the first three letters of the month.
        String shortMonth;
        shortMonth = month.toLowerCase().substring(0, 3);

        // Could use a switch case statement here, but the String version isn't compatible with Android...
        if (shortMonth.equals("jan"))
            return Calendar.JANUARY;
        else if (shortMonth.equals("feb"))
            return Calendar.FEBRUARY;
        else if (shortMonth.equals("mar"))
            return Calendar.MARCH;
        else if (shortMonth.equals("apr"))
            return Calendar.APRIL;
        else if (shortMonth.equals("may"))
            return Calendar.MAY;
        else if (shortMonth.equals("jun"))
            return Calendar.JUNE;
        else if (shortMonth.equals("jul"))
            return Calendar.JULY;
        else if (shortMonth.equals("aug"))
            return Calendar.AUGUST;
        else if (shortMonth.equals("sep"))
            return Calendar.SEPTEMBER;
        else if (shortMonth.equals("oct"))
            return Calendar.OCTOBER;
        else if (shortMonth.equals("nov"))
            return Calendar.NOVEMBER;
        else if (shortMonth.equals("dec"))
            return Calendar.DECEMBER;
        else
            return Calendar.UNDECIMBER;
    }
}

Related Tutorials