Android 在基于时间的文本视图中设置文本

Android 在基于时间的文本视图中设置文本,android,Android,我想在文本视图中显示消息,我现在使用的代码是 mText = (TextView) findViewById(R.id.textView19); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String str = sdf.format(new Date()); String[] hr=str.split(":"); int hr1=Integer.pars

我想在文本视图中显示消息,我现在使用的代码是

mText = (TextView) findViewById(R.id.textView19);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String str = sdf.format(new Date());

        String[] hr=str.split(":");

        int hr1=Integer.parseInt(hr[0]);

        if(hr1<12)
        {
            mText.setText("it's morning!");
        }else if(hr1>12&& hr1<17)
        {
            mText.setText("it's afternoon!");
        }else if(hr1>17&& hr1<20)
        {
            mText.setText("it's evening!");
        }
mText=(TextView)findViewById(R.id.textView19);
SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss”);
字符串str=sdf.format(newdate());
字符串[]hr=str.split(“:”);
inthr1=Integer.parseInt(hr[0]);

如果(hr112&&hr117&&hr1您正在创建空日期,则需要通过执行以下操作获取当前时间:

Calendar c = Calendar.getInstance(); // Get current time
int hr1 = c.get(Calendar.HOUR_OF_DAY); // Gets the current hour of the day from the calendar created ( from 1 to 24 )

查看日历文档。

比您更适合!需要等待四分钟,我才能确认您的答案。没问题,请记住Java文档确实很有用,您几乎总能找到您需要的答案!您的代码应该可以运行,同时输入其他条件,以检查您的书面条件是否正确不管你是不是。