如何将日期时间字符串转换为整数数据类型?

编程入门 行业动态 更新时间:2024-10-26 14:40:35
本文介绍了如何将日期时间字符串转换为整数数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 String timerStop1 = String.format("%02d", hours) + ":"+String.format("%02d", minutes) + ":" + String.format("%02d", seconds);

我正在使用上面的串联字符串值,我想将其转换为整数在我的Android应用程序中使用。我该怎么做?

I'm taking the above concatenated string value and I want to convert it into an integer to use in my android application. How do I do this?

推荐答案

第一步是将时间换成 HH:MM:SS 格式(这是你的字符串的格式)到秒的格式:

A first step will be to convert the time in HH:MM:SS format (which is how your string is formatted) to that of seconds as per the following:

String timerStop1 = String.format("%02d", hours) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds); String[] timef=timerStop1.split(":"); int hour=Integer.parseInt(timef[0]); int minute=Integer.parseInt(timef[1]); int second=Integer.parseInt(timef[2]); int temp; temp = second + (60 * minute) + (3600 * hour); System.out.println("seconds " + temp);

但是,这只会将时间作为秒(整数),而不是时间戳!

更新:

而且,正如科林指出的那样,鉴于您已经拥有访问权限变量:小时,分钟,秒 - 为什么不像他建议的那样 - 这是完全正确的?

And, as Colin pointed out, given that you already have access to the variables: hours, minutes, seconds - why not do it like what he suggested - which is completely correct?

stackoverflow/a/15307211/866930

那是因为OP想要知道如何将HH:MM:SS字符串转换为整数 - 如果是这样,那么这是最通用的方式,IMO。

That's because the OP wants to know how to convert an HH:MM:SS string to an integer - if so, then this is the most general way in which to do so, IMO.

更多推荐

如何将日期时间字符串转换为整数数据类型?

本文发布于:2023-11-17 06:43:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   转换为   字符串   如何将   数据类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!