将字符串月份转换为整数java

Convert String month to integer java(将字符串月份转换为整数java)
本文介绍了将字符串月份转换为整数java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

How do I convert a month string to integer?

在点击方法时,我想显示所选日期,但如果日期有事件,它应该显示更多关于该事件的内容.检查假日事件的方法需要整数值.

On click method I want to display the date which is selected but if the date has an event it should display something more about the event. The method to check for the holiday event requires integer values.

代码如下:

更新:

@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;
    }

推荐答案

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

  • IDE 一个演示

这篇关于将字符串月份转换为整数java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)