如何使用Textview中的日期和时间设置闹钟管理器

问题描述:

我将timestamp转换为date and time并将结果设置为textview如何使用Textview中的日期和时间设置闹钟管理器

例如1443884578Sat 3 October 2015 18:02

我想设置上述date and timealarm manager。经过研究,我发现使用日期时间选择器的码。

public void onDateSelectedButtonClick(View v) { 
 
     // Get the date from our datepicker 
 
     int day = picker.getDayOfMonth(); 
 
     int month = picker.getMonth(); 
 
     int year = picker.getYear(); 
 
     // Create a new calendar set to the date chosen 
 
     // we set the time to midnight (i.e. the first minute of that day) 
 
     Calendar c = Calendar.getInstance(); 
 
     c.set(year, month, day); 
 
     c.set(Calendar.HOUR_OF_DAY, 0); 
 
     c.set(Calendar.MINUTE, 0); 
 
     c.set(Calendar.SECOND, 0); 
 
     // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service 
 
     scheduleClient.setAlarmForNotification(c); 
 
     // Notify the user what they just did 
 
     Toast.makeText(this, "Notification set for: " + day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show(); 
 
    }

但其只获得了date并触发alarm日发生分钟。

问题:我想从我的textviewdatetime并跳过格式我有这个日期时间选择器。这可能吗?

String input = "Sat October 3 2015 18:02"; // Instead of String input = "Mon Feb 06 2015"; 

    Calendar cal = Calendar.getInstance(); 
    Date date = new Date(); 
// Changed the format to represent time of day 
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH); 
      try { 
       date = sdf.parse(input); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 

    cal.setTime(date); 
    //We haven't parsed the seconds from the original date so this will result 
    //in 18:02:00 - 10seconds. 
    //For a correct calculation, you could parse the seconds as well 
    //See SimpleDateFormat above, but you would have to provide the original date 
    //with seconds as well 
    cal.add(Calendar.SECOND, -10); 
    scheduleClient.setAlarmForNotification(cal); 
+0

感谢您的建议..我在这里设置什么,而不是c - > scheduleClient.setAlarmForNotification(c); –

+1

没问题。我修改了答案,希望它满足您的需要 –

+0

try-catch块中的代码有错误,“日历中的set time(java.until.Date)不能应用于(java.text.SimpleDateFormat,java。 until.Locale) –