分钟之间的差异 [英] Difference between minutes

查看:27
本文介绍了分钟之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下格式的 2 个字符串:

09/21 10:06AM"09/21 10:10AM"

如何找到这些字符串之间的时差,存储为 int?这必须足够强大才能处理上午 10:59 和 11:02(中间的奇数分钟)、上午 11:59 和下午 12:03(上午到下午切换)等情况.无需担心秒数.

谢谢!

解决方案

我建议:

  • 使用 Joda Time 而不是内置 API;它好多了.
  • 解析为LocalDateTime
  • 找出它们之间的区别:

    Minutes period = Minutes.minutesBetween(first, second);int 分钟 = period.getMinutes();

Say I have 2 strings in the following format:

"09/21 10:06AM" "09/21 10:10AM"

How do I find the time difference between these strings, stored as an int? This has to be robust enough to handle situations like 10:59AM and 11:02AM (odd number of minutes in between), 11:59AM and 12:03PM (AM to PM switch) etc. No need to worry about seconds.

Thanks!

解决方案

I would suggest:

  • Use Joda Time instead of the built-in API; it's much nicer.
  • Parse into LocalDateTime values
  • Find the difference between them with:

    Minutes period = Minutes.minutesBetween(first, second);
    int minutes = period.getMinutes();
    

这篇关于分钟之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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