Using setText with rules regarding the current time

ぐ巨炮叔叔 提交于 2019-12-06 15:08:55

It might be a bit more complicated. I would probably use the following approach (withh a bit of optimization ofc.)

    TextView textView = (TextView) findViewById(R.id.text);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());

    cal.set(Calendar.HOUR_OF_DAY, 8);
    cal.set(Calendar.MINUTE, 45);
    cal.set(Calendar.SECOND, 0);

    long morning_start = cal.getTimeInMillis();

    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 0);

    long morning_end = cal.getTimeInMillis();
    long now = System.currentTimeMillis();

    if(now > morning_start && now < morning_end)
    {
        textView.setText("Good morning");
    }
    else
    {
        textView.setText("Have a nice day");
    }

Use the time class to get the current time. Then use switch statements or if/else if/else blocks (depending on your exact requirements) to display different texts.

You could use a Calender to get the current hour of the day.

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!