i want to set a time for alarm .but when i get time value from db it is in string format .so tell me what i can do for this . i am new here and in android plz ignore any mistake the code is
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);
Do this you can convert string to 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();
}
Hope this helps.
String s= names.get(i).getTime();
System.out.println("Value: " + s);
//value of s is 11:50 AM
Printed value is in "hh:mm a" format.
So you should write as
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);
Try this code
Date m_date = new Date();
SimpleDateFormat m_dateFormat = new SimpleDateFormat("hh:mm a");
m_date = m_dateFormat.parse(s);
m_date.getTime();
I checked below code & it is working fine on my side
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.