我想比较系统时间和用户输入时间,我比较但我不知道它的正确或错误的陈述,因为如果(检查)没有在这里运行是我的代码
c = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat(" hh:mm a");
String strdate = df1.format(c.getTime());
Toast.makeText(getApplicationContext(), strdate , Toast.LENGTH_LONG).show();
以上Toast行显示系统的当前时间,即04:50 AM
Toast.makeText(getApplicationContext(), prayerTimes.get(0) , Toast.LENGTH_LONG).show();
此吐司线显示用户输入的时间,即06:30 AM
if(strdate ==prayerTimes.get(0))
{
Intent in = new Intent(MainActivity.this, Dialog.class);
startActivity(in);
finish();
Toast.makeText(getApplicationContext(), "yahoo", Toast.LENGTH_LONG).show();
}
当两个时间都相同时如果检查不起作用...请帮助我如何比较两次?
答案 0 :(得分:1)
非常简单,以毫秒或分钟转换值time1和time2,并测量差异。
答案 1 :(得分:0)
使用equals()
代替==,因为您正在比较字符串。
if(strdate.equals(prayerTimes.get(0)))
{
Intent in = new Intent(MainActivity.this, Dialog.class);
startActivity(in);
finish();
Toast.makeText(getApplicationContext(), "yahoo", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
我建议你不要比较两个字符串,而是将两个时间转换成长,然后比较它。要获得当前时间
long currentTime = System.currentTimeMillis();
在下面的函数中传递您的用户时间以获得用户以毫秒为单位的时间,然后比较两者
private long getUserTime(String time){
SimpleDateFormat df1 = new SimpleDateFormat(" hh:mm a");
long timeInms = 0;
Date date = null;
try {
date = df1.parse(time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
timeInms = date.getTime();
return timeInms;
}
答案 3 :(得分:0)
public class NotificationService extends Service {
private Intent myIntent;
MaterialDialog dialog;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.myIntent = intent;
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run() {
handler.postDelayed(this, 1000);
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
String currentDate = formatter1.format(calendar1.getTime());
String dateString = "11/08/2017";
String timeString = "07:00";
String datadb = dateString + " " + timeString;
//comparing with the exac date/time/year what ever your wish
if (currentDate.compareTo(datadb) >= 0) {
showDialog("msg"); // you can send extra information to service via intent
}
}
};
handler.postDelayed(runnable, 1000);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getApplicationContext(), "in service created", Toast.LENGTH_LONG).show();
}
private void showDialog(String message) {
// NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.holo_meducate)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.pdf_banner))
.setContentTitle(msg)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml(description)))
.setContentText(Html.fromHtml(description))
.setSound(getNotificationSoundUri());
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
mNotificationManager.notify(num, mBuilder.build());
setLatestNotificationFlag();
}
@Override
public void onDestroy() {
if (dialog != null) {
dialog.dismiss();
}
super.onDestroy();
}

启动服务并在服务内部启动一个可循环的thrade in循环,每30秒重新发生一次,并将系统时间与所需时间进行比较,这将自动触发通知......