2017-02-02 80 views
-2

我是新来的机器人,我想学习一些新东西。Android Textview随着时间的变化而变化

我想在时间改变时更改我的textview。 例如,如果是中午12点到12点之间,那么它应该表现出“早上好”,否则它应该显示“晚上好”,我试图

代码如下..

SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
String formattedDate = df.format(c.getTime()); 

Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); 

if(formattedDate.equals("00:00:00")&&formattedDate.equals("12:00:00")){ 
    textView.setText("good morning"); 
}else{ 
    textView.setText("good Evening"); 
} 

的问题这段代码是,如果你在上午12点或者下午12点打开这个应用程序,它会显示文字“早上好”,如果不是,那么它会显示晚上好。 我想要的是文本应该显示上午12点到12点早上好,对于其余的时间,它应该显示晚上好..

如果你能帮助那么谢谢我N使.. :)

+1

参阅[检查是否给定的时间处于两个时间之间不管(http://stackoverflow.com/questions/17697908/check-if-a-given-time-lies-between-two-times-regardless-of-date) –

+0

可能有[Ho我可以确定一个日期是否在Java中的两个日期?](http://stackoverflow.com/questions/883060/how-can-i-determine-if-a-date-is-between-two-dates- in-java) – akash93

+0

Akash93与所有尊重..根据我这个问题是关于时间不约会...我想改变我的textview根据时间..不按日期.. –

回答

0

检查下面的代码

public static String Convert24to12(String time) 
{ 
String convertedTime =""; 
try { 
    SimpleDateFormat displayFormat = new SimpleDateFormat("hh:mm a"); 
    SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm:ss"); 
    Date date = parseFormat.parse(time);   
    convertedTime=displayFormat.format(date); 
    System.out.println("convertedTime : "+convertedTime); 
} catch (final ParseException e) { 
    e.printStackTrace(); 
} 
return convertedTime; 
//Output will be 10:23 PM 
} 

在这里你只需要检查这个条件

if(Convert24to12(formattedDate).contains("AM")){ 
    textView.setText("good morning"); 
}else{ 
    textView.setText("good Evening"); 
} 
1
Calendar c = Calendar.getInstance(); 
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss a"); 
    String formattedDate = df.format(c.getTime()); 

    if (formattedDate.contains("AM")) { 
     textView.setText("Good Morning"); 
    } else { 
     textView.setText("Good Evening"); 
    } 
0
Calendar calendar = Calendar.getInstance(); 

SimpleDateFormat sdf = new SimpleDateFormat("HH"); 

int temp = Integer.parseInt(sdf.format(calendar.getTimeInMillis())); 

if(temp > 12) 
{ 
    System.out.println("Good Evening"); 
} 
else 
{ 
    System.out.println("Good Morning"); 
} 
相关问题