无法解析的日期:“10:40:54”(偏移量为 0)
Unparseable date: "10:40:54" (at offset 0)
我从 1970 年开始就以毫秒为单位,我正在将它转换为格式为 "EEEE-yyyy/MM/dd-hh:mm:ss"
的字符串,然后将其拆分为数组,并尝试解析 hh:mm:ss
部分至今,所以我感到很沮丧使用 date.compareTo
方法,但它给了我无法解析的日期异常。
public class NotificationWakefulIntentService extends IntentService {
public NotificationWakefulIntentService() {
super("NotificationWakefulIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("time",(System.currentTimeMillis()/1000)+"");
Date dateTemp = null;
Date dateTempLocal = null;
List<Action> actionList = Action.listAll(Action.class);
for (Action act: actionList){
if (act.isRepeating()){
SimpleDateFormat formatter = new SimpleDateFormat("EEEE-yyyy/MM/dd-hh:mm:ss", Locale.getDefault());
String asString = formatter.format(act.getDate());
String[] parsedDate = asString.split("-");
Log.e("time", asString);
String time = parsedDate[2];
//String date = parsedDate[1];
String day = parsedDate[0];
String asStringLocal = formatter.format(System.currentTimeMillis());
String[] parsedDateLocal = asStringLocal.split("-");
String timeLocal = parsedDateLocal[2];
//String dateLocal = parsedDateLocal[1];
String dayLocal = parsedDateLocal[0];
try {
dateTemp = formatter.parse(time);
dateTempLocal = formatter.parse(timeLocal);
Log.e("dateEquality", (dateTemp.compareTo(dateTempLocal))+"");
} catch (ParseException e) {
e.printStackTrace();
}
if (dateTemp!=null && dateTempLocal!=null) {
if (day.equals(dayLocal) && dateTemp.compareTo(dateTempLocal) > 0) {
Log.e("notify", "notify");
}
}
}else {
}
}
SimpleWakefulReciever.completeWakefulIntent(intent);
}
}
LogCat 和数据日志
E/time: понедельник-2016/10/24-10:40:54
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: java.text.ParseException: Unparseable date: "10:40:54" (at offset 0)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at java.text.DateFormat.parse(DateFormat.java:626)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at by.mtz.reminder.wakefulService.NotificationWakefulIntentService.onHandleIntent(NotificationWakefulIntentService.java:58)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.Looper.loop(Looper.java:137)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.HandlerThread.run(HandlerThread.java:60)
10-24 09:27:06.805 2655-3407/by.mtz.reminder D/time: 1477290426
您的格式字符串是错误的。必须是:new SimpleDateFormat("hh:mm:ss");
仅解析时间部分。
所以在您的代码中,您需要一个用于解析时间,一个用于格式化您的字符串
我从 1970 年开始就以毫秒为单位,我正在将它转换为格式为 "EEEE-yyyy/MM/dd-hh:mm:ss"
的字符串,然后将其拆分为数组,并尝试解析 hh:mm:ss
部分至今,所以我感到很沮丧使用 date.compareTo
方法,但它给了我无法解析的日期异常。
public class NotificationWakefulIntentService extends IntentService {
public NotificationWakefulIntentService() {
super("NotificationWakefulIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("time",(System.currentTimeMillis()/1000)+"");
Date dateTemp = null;
Date dateTempLocal = null;
List<Action> actionList = Action.listAll(Action.class);
for (Action act: actionList){
if (act.isRepeating()){
SimpleDateFormat formatter = new SimpleDateFormat("EEEE-yyyy/MM/dd-hh:mm:ss", Locale.getDefault());
String asString = formatter.format(act.getDate());
String[] parsedDate = asString.split("-");
Log.e("time", asString);
String time = parsedDate[2];
//String date = parsedDate[1];
String day = parsedDate[0];
String asStringLocal = formatter.format(System.currentTimeMillis());
String[] parsedDateLocal = asStringLocal.split("-");
String timeLocal = parsedDateLocal[2];
//String dateLocal = parsedDateLocal[1];
String dayLocal = parsedDateLocal[0];
try {
dateTemp = formatter.parse(time);
dateTempLocal = formatter.parse(timeLocal);
Log.e("dateEquality", (dateTemp.compareTo(dateTempLocal))+"");
} catch (ParseException e) {
e.printStackTrace();
}
if (dateTemp!=null && dateTempLocal!=null) {
if (day.equals(dayLocal) && dateTemp.compareTo(dateTempLocal) > 0) {
Log.e("notify", "notify");
}
}
}else {
}
}
SimpleWakefulReciever.completeWakefulIntent(intent);
}
}
LogCat 和数据日志
E/time: понедельник-2016/10/24-10:40:54
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: java.text.ParseException: Unparseable date: "10:40:54" (at offset 0)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at java.text.DateFormat.parse(DateFormat.java:626)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at by.mtz.reminder.wakefulService.NotificationWakefulIntentService.onHandleIntent(NotificationWakefulIntentService.java:58)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.Looper.loop(Looper.java:137)
10-24 09:27:01.265 2655-3291/by.mtz.reminder W/System.err: at android.os.HandlerThread.run(HandlerThread.java:60)
10-24 09:27:06.805 2655-3407/by.mtz.reminder D/time: 1477290426
您的格式字符串是错误的。必须是:new SimpleDateFormat("hh:mm:ss");
仅解析时间部分。
所以在您的代码中,您需要一个用于解析时间,一个用于格式化您的字符串