我想设置一个闹钟时间。但是当我从数据库获取时间值时,它是字符串格式的。所以请告诉我我可以为此做什么。 我是新来的,在 Android 中请忽略任何错误 代码是
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();
}
希望这有帮助。
关于java - 将字符串转换为时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29507938/