简体   繁体   English

将String转换为时间格式

[英]Convert String in to time format

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 . 我想设置一个警报时间。但是当我从db获取时间值时,它是字符串格式。所以告诉我该怎么做。 i am new here and in android plz ignore any mistake the code is 我是新来的,在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);

Do this you can convert string to timeinMillis: 这样做可以将字符串转换为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. 打印的值采用“ hh:mm a”格式。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM