2

如何将月份字符串转换为整数?

在单击方法上,我想显示所选日期,但如果日期有事件,它应该显示更多关于该事件的信息。检查假日事件的方法需要整数值。

这是代码:

更新:

@Override
public void onClick(View view)
    {
        int m = 0;
        int d, y;
        String date_month_year = (String) view.getTag();
        selectedDayMonthYearButton.setText("Selected: " + date_month_year);

        String [] dateAr = date_month_year.split("-");
        Log.d(tag, "Date Split: " + dateAr[0] + " " + dateAr[1] + " " + dateAr[2]);
        y = Integer.parseInt(dateAr[2]);



        try {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(new SimpleDateFormat("MMM").parse(dateAr[1]));
            m = c1.get((Calendar.MONTH) + 1);
        } 
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //int m = Integer.parseInt(dateAr[1]);
        d = Integer.parseInt(dateAr[0]);
        Log.d(tag, "Date forward: " + d + " " + m + " " + y);

        if (isHoliday(d, m, y))
        {
            Intent i = new Intent(view.getContext(), HijriEvents.class);
            i.putExtra("date_string", date_month_year);
            startActivity(i);

        }


        try
            {
                Date parsedDate = dateFormatter.parse(date_month_year);
                Log.d(tag, "Parsed Date: " + parsedDate.toString());

            }
        catch (ParseException e)
            {
                e.printStackTrace();
            }
    }

    public Boolean isHoliday(int d, int m, int y)
    {
        HijriCalendar hijri = new HijriCalendar(y, m, d);
        if (!hijri.checkIfHoliday().contentEquals(""))
            return true;
        return false;
    }
4

2 回答 2

13
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("MMM").parse("July"));
int monthInt = cal.get(Calendar.MONTH) + 1;

于 2012-07-06T21:39:16.893 回答
1

只有 12 个月,所以只需规范化字符串并进行字符串比较。我不会尝试过度优化。

于 2012-07-06T21:37:52.550 回答