2016-04-29 67 views
0

我需要創建具有特定日期和時間的日曆。創建具有特定日期和時間的日曆

private void setNotification(String date, String time) { 
    Log.i("log", "1"); 
    //Get date & time 
    String[] dateArr = date.split("."); 
    String[] timeArr = date.split(":"); 
    Log.i("log", "2"); 
    //Set calender to lesson's date & time 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Integer.valueOf(dateArr[2]), Integer.valueOf(dateArr[1]) - 1, Integer.valueOf(dateArr[0]), Integer.valueOf(timeArr[0]), Integer.valueOf(timeArr[1])); 
    Log.i("log", "3"); 

    //Schedule notification 
    scheduleNotification(CreateNewLesson.this, cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(), Integer.valueOf(dateArr[0] + dateArr[1] + dateArr[2] + timeArr[0] + timeArr[1])); 
} 

日期字符串格式爲DD.MM.YYYY,時間爲HH:MM,當我打電話的功能,我可以看到「2」的日誌,但不是「3」。

這是爲什麼?

+0

我們只能推測爲什麼你會觀察你所做的行爲,因爲你所提供的代碼是不完整的,而且你沒有包含任何實際的樣本輸入。如果你想回答你提出的問題,然後提出[mcve]。 –

+0

使用正確的任務工具,不要自己解析日期。 – Tunaki

回答

0

您可以使用SimpleDateFormat解析日期和時間。類似於

DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm"); 
Calendar cal = Calendar.getInstance(); 
try { 
    cal.setTime(sdf.parse(String.format("%s %s", date, time))); 
    // use cal.... 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 
相關問題