我想设置闹钟的时间。但是当我从db获得时间值时,它是字符串格式。请告诉我我能为此做些什么。 我是新来的,在android plz忽略任何错误 代码是
String s= names.get(i).getTime();
System.out.println("Value: " + s);
//value of s is 11:50 AM
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent =PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, 0);
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, s.getTimeInMillis(), pendingIntent);
答案 0 :(得分:2)
这样做可以将字符串转换为timeinMillis:
String givenDateString =s;
Log.d("Pana", "The value of s is " + s);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0530"));
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
Log.d("Pana ", "Date in milli :: " + timeInMilliseconds);
Log.i("TAG", "System.currentTimeMillis() = " + System.currentTimeMillis());
} catch (ParseException e) {
e.printStackTrace();
}
希望这有帮助。
答案 1 :(得分:1)
String s= names.get(i).getTime();
System.out.println("Value: " + s);
//value of s is 11:50 AM
印刷值在" hh:mm a"格式。
所以你应该写为
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
d = df.parse(s);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent =PendingIntent.getBroadcast(getBaseContext(),
RQS_1, intent, 0);
AlarmManager alarmManager(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, d.getTime(), pendingIntent);
答案 2 :(得分:0)
试试此代码
Date m_date = new Date();
SimpleDateFormat m_dateFormat = new SimpleDateFormat("hh:mm a");
m_date = m_dateFormat.parse(s);
m_date.getTime();
我查了下面的代码&它在我身边工作正常
Date m_date = new Date();
SimpleDateFormat m_dateFormat = new SimpleDateFormat("hh:mm a");
try
{
m_date = m_dateFormat.parse("11:50 AM");
}
catch (ParseException p_e)
{
p_e.printStackTrace();
}
m_date.getTime();