[英]Convert String in to time format
我想設置一個警報時間。但是當我從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);
這樣做可以將字符串轉換為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();
}
希望這可以幫助。
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);
試試這個代碼
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.