如何在Android中将字符串转换为时间(分钟) [英] How to Convert string to time (Minutes) in Android

查看:999
本文介绍了如何在Android中将字符串转换为时间(分钟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个编辑文本,用户可以输入(3天或2小时或5个月),



它需要把它转换成几分钟..

我不知道,请指导我













提前致谢

Arahv.M

Hi

I am having an edit text where user can enter like ("3 days" or "2 hour" or "5 months")whatever,

It needs to convert it to minutes..
I have no idea , please guide me






Thanks in advance
Arahv.M

推荐答案

创建一个表格,将描述映射到分钟数或秒数。例如

Create a table that maps the descriptions to the number of minutes or seconds. e.g
Day = 1440
Hour = 60
...



Split将字符串放入其组成部分。

从转换表中获取分钟数。

乘以数字,即分钟数。

将其转换为显示所需的任何格式。


Split the string into its component parts.
Get the number of minutes from the conversion table.
Multiply that by the number and that is the number of minutes.
Convert that to whatever format you need for the display.


String time = "530pm";
SimpleDateFormat dateFormat = new SimpleDateFormat("hmmaa"); 
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm aa");
try {
    Date date = dateFormat.parse(time);

    String out = dateFormat2.format(date);
    Log.e("Time", out);
} catch (ParseException e) {
}







试试这个




Try this


这篇关于如何在Android中将字符串转换为时间(分钟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆