【问题标题】:Get The Last Number of the Clock Time as Integer以整数形式获取时钟时间的最后一个数字
【发布时间】:2016-01-10 01:29:56
【问题描述】:

我在这里问的不是很常见,我什至不确定这是否可能,

我知道如何使用此代码获取当前设备时间(以字符串形式返回):

SimpleDateFormat Date = new SimpleDateFormat("hh:mm");
String DateTime = Date.format(new Date());

我正在寻找的是获取分钟的最后一个整数,

例如。 : 时间是 7:24 所以我想把 4(Last Number) 作为一个整数,

有可能,如果有,怎么做?

【问题讨论】:

    标签: android time clock


    【解决方案1】:

    您也可以不使用 SimpleDateFormat,但使用 Calendar

    int listDigit = Calendar.getInstance().get(Calendar.MINUTE) % 10;
    
    int listDigit = Data.getDate().getMinutes % 10; // deprecated
    

    【讨论】:

    • getMinutes() on java.util.date 已弃用
    • @AlbAtNf 是的,我记得我发布的第二个......我将它编辑到日历
    • 添加.getInstance(),我很高兴! :P
    • 这项工作也可以,正如您所提到的,通过首先获取日历实例,我在我的所有代码中都使用 SimpleDateFormat,但我不接受您的答案,因为这更短,谢谢
    • 我测试过,这是第二快的解决方案。 Substing真的很慢。使用 System.currentTimeMillis() 计算通常比这个解决方案快一点
    【解决方案2】:

    我在 cmets 中回答这个问题的声誉并不高,但我认为您要做的就是在变量中获取秒数并创建子字符串并将该字符串转换为整数。

    【讨论】:

    • 这是我在您发表评论之前的想法,谢谢
    【解决方案3】:

    我会回答自己,解决方案很简单:

        SimpleDateFormat Date = new SimpleDateFormat("hh:mm");
        String DateTime = Date.format(new Date());
    
        int integer= Integer.parseInt(DateTime.substring(DateTime.length() - 1));
    
        Toast.makeText(this, String.valueOf(integer), Toast.LENGTH_LONG).show();
    

    谢谢

    【讨论】:

      【解决方案4】:
      dateTime.substring(dateTime.length() - 1, dateTime.length())
      

      *旁注 - Java 变量名称的约定是以小写字母开头。您在变量名中的第一个字母使用了大写。 Date.format 可能会让某些人感到困惑,因为它看起来像是对 Date 类中的静态方法的引用。

      【讨论】:

        【解决方案5】:

        试试这个:

        int lastMin = (int)((System.currentTimeMillis()/60000l) % 10);
        

        16:17 给我 7 点

        【讨论】:

        • 我已经回答了,感谢您的帮助
        猜你喜欢
        • 2020-04-05
        • 1970-01-01
        • 2019-02-12
        • 2017-05-27
        • 1970-01-01
        • 2012-11-28
        • 2012-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多