【问题标题】:how to print last 4 digits from my 13 digit long如何从我的 13 位长中打印最后 4 位
【发布时间】:2015-12-21 13:10:54
【问题描述】:
package com.nusecond.code;

public class Code {



    public static void main(String args[]) {

        Long i=System.currentTimeMillis();
        System.out.println(i);
    }

}

最后 4 位数字

  1. 我在这里尝试以毫秒为单位获取当前时间的最后 4 位数字
  2. 上面的代码给出了 13 位长类型数字,我想要最后 4 个

【问题讨论】:

标签: java


【解决方案1】:
public class Main {
  public static void main(String[] args) {
    long wtf = 10516;
    System.out.println(
      Long.toString(wtf).substring(
       Long.toString(wtf).length() - 4)); // LAST 4 DIGITS
  }
}

输出:0516

【讨论】:

    【解决方案2】:

    如下使用“模”运算符%

        Long i = System.currentTimeMillis();
        // Here we format the result to force 4 digits starting with a 0
        // if the long is shorter than 4 digits long (i.e. in case your
        // long looks like 1, 11 or 111 as numbers cannot start with the
        // 0 digit)
        String result = String.format("%04d", i % 10000);
    

    【讨论】:

    • 这是因为 long 不能以 0 开头。编辑后的答案现在格式化输出。
    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多