gpt4 book ai didi

java - 无法为我的公历设置所需的时间

转载 作者:行者123 更新时间:2023-12-02 12:00:29 25 4
gpt4 key购买 nike

下面代码中的问题是我试图将 EVENING 时间设置为下午 6 点。请忽略 Calendar.evening 值,因为它用于测试目的。这是我的日志文件

D/Notification: MORN Tue Nov 14 16:00:00 GMT+05:30 2017 vs EVE Tue Nov 14 16:00:00 GMT+05:30 2017

MORN: Tue Nov 14 16:00:00 GMT+05:30 2017 vs Tue Nov 14 16:00:00 GMT+05:30 2017 EVE: Tue Nov 14 16:00:00 GMT+05:30 2017 vs Tue Nov 14 16:00:00 GMT+05:30 2017

现在的问题是两个 getTime() 都返回当前时间。

我希望 IF LOOP 在下午 4 点和上午 8 点执行,对于这个例子,我的逻辑似乎非常合法!请指出错误

public class MyNotificationService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}


static Calendar eve,morn;

@Override
public void onCreate() {
super.onCreate();
morn=Calendar.getInstance();
morn.set(Calendar.HOUR_OF_DAY, 4);
morn.set(Calendar.MINUTE, 0);
morn.set(Calendar.SECOND, 0);
morn.set(Calendar.AM_PM,1);

eve=Calendar.getInstance();
eve.set(Calendar.HOUR_OF_DAY, 6);
eve.set(Calendar.MINUTE, 0);
eve.set(Calendar.SECOND, 0);
eve.set(Calendar.AM_PM,1);

Log.d("Notification", "MORN "+morn.getTime().toString() +" vs EVE "+eve.getTime().toString());

}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

setTimer();

return Service.START_STICKY;
}



void setTimer(){

Timer timer = new Timer ();
TimerTask hourlyTask = new TimerTask () {

@Override
public void run () {
Calendar now=Calendar.getInstance();
now.set(Calendar.MINUTE,0);
now.set(Calendar.SECOND,0);


Log.d("Notification MORN", morn.getTime().toString() +" vs "+now.getTime().toString());
if (now.compareTo(morn)==0||now.getTime().equals(morn.getTime())) {
Log.d("Notification Func", eve.getTime().toString() + " MORNING exe");
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
if (isInternetOn(getApplicationContext())){
getCaseDetails();
}else {
setNotification("Internet not available","Please check your Cases");
}
}
};
mainHandler.post(myRunnable);
}
Log.d("Notification EVE", eve.getTime().toString() +" vs "+now.getTime().toString());
if (now.compareTo(eve)==0||now.getTime().equals(eve.getTime())) {
Log.d("Notification Func", eve.getTime().toString() + " EVENING exe");
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
getCaseDetails();
}
};
mainHandler.post(myRunnable);
}
}
};
// timer.schedule (hourlyTask, 0L, 1000*60*60);
timer.schedule (hourlyTask, 0L, 30*1000);
}


void getDetails() {
}



@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Notification Service Stopped", Toast.LENGTH_LONG).show();
}



public void setNotification(String titleStr,String descStr) {

setting alarm here.
}

}

最佳答案

使用 AM/PM 需要使用 Calendar.HOUR,而不是 Calendar.HOUR_OF_DAY,设置 AM/PM 时会忽略它:

Calendar morn = Calendar.getInstance();
morn.set(Calendar.HOUR_OF_DAY, 4);
morn.set(Calendar.MINUTE, 0);
morn.set(Calendar.SECOND, 0);
morn.set(Calendar.AM_PM, Calendar.PM);

System.out.println(someDateFormat.format(morn.getTime()));
// current hour

morn = Calendar.getInstance();
morn.set(Calendar.HOUR, 4);
morn.set(Calendar.MINUTE, 0);
morn.set(Calendar.SECOND, 0);
morn.set(Calendar.AM_PM, Calendar.PM);

System.out.println(someDateFormat.format(morn.getTime()));
// correct hour

关于java - 无法为我的公历设置所需的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com