我想得到当前时间并将其与我提供的时间范围进行比较 例如,如果当前时间是在上午6:00到11:59之间,我想设置早安的问候语消息
以下是我的Android应用程序的一些代码
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class MainActivity extends Activity implements View.OnClickListener {
protected Calendar firstBound;
protected Calendar secondBound;
protected static Calendar cal;
protected static String timeString = null;
protected static String greetString = null;
protected static DateFormat date;
Button greetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get a reference to the greetButton on the UI
greetButton = (Button) findViewById(R.id.greetButton);
cal = Calendar.getInstance(TimeZone.getDefault());
Date currentLocalTime = cal.getTime();
date = new SimpleDateFormat("HH:mm");
timeString = date.format(currentLocalTime);
char h1 = timeString.charAt(0);
char h2 = timeString.charAt(1);
char m1 = timeString.charAt(3);
char m2 = timeString.charAt(4);
cal.set(Calendar.HOUR,Character.getNumericValue(h1) + Character.getNumericValue(h2));
cal.set(Calendar.MINUTE,Character.getNumericValue(m1) + Character.getNumericValue(m2));
// Set the onClickListener for the greetButton to be this class.
// This requires that the class implement the View.OnClickListener callback
// the onClick() method
greetButton.setOnClickListener(this);
}
我的On Click方法是
public void onClick(View v) {
TextView textMessage = (TextView) findViewById(R.id.textMessage);
EditText editFriendName = (EditText) findViewById(R.id.editFriendName);
String friendName = editFriendName.getText().toString();
switch (v.getId()) {
case R.id.greetButton:
// set the string being displayed by the TextView to the greeting
if(cal.after(setFirstBoundLimits(14, 00)) && cal.before(setSecondBoundLimits(18, 00))){
Toast.makeText(MainActivity.this, timeString, Toast.LENGTH_LONG).show();
}
// message for the friend
//textMessage.setText(getString(R.string.greetstring) + friendName + "!");
break;
default:
break;
}
}
private Calendar setFirstBoundLimits(int hh, int mm){
firstBound = Calendar.getInstance();
firstBound.set(Calendar.HOUR, hh);
firstBound.set(Calendar.MINUTE, mm);
return firstBound;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
****谢谢......这段代码真的对我有用
protected String getCurrentTimeMessage(){
//Date date = new Date();
Calendar cal = Calendar.getInstance();
//cal.setTime(date);
int hours = cal.get(Calendar.HOUR_OF_DAY);
if(hours>=6 && hours<=11){
//Toast.makeText(this, "Good Morning" + hours, Toast.LENGTH_SHORT).show();
return "Good Morning";
}else if(hours>=12 && hours<=16){
// Toast.makeText(this, "Good Afternoon"+ hours, Toast.LENGTH_SHORT).show();
return "Good Afternoon";
}else if(hours>=17 && hours<=20){
// Toast.makeText(this, "Good Evening"+ hours, Toast.LENGTH_SHORT).show();
return "Good Evening";
}else if(hours>=21 && hours<=23){
// Toast.makeText(this, "Good Night"+ hours, Toast.LENGTH_SHORT).show();
return "Good Night";
}
else if(hours >=0 && hours <= 5){
return "Good Night";
}
return null;
}