Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印我的13位数长度中的最后4位数 最后4位数字 在这里,我试图以毫秒为单位获取当前时间的最后4位数字 上面的代码给出了13位长的类型编号,我想要最后4位_Java - Fatal编程技术网

Java 如何打印我的13位数长度中的最后4位数 最后4位数字 在这里,我试图以毫秒为单位获取当前时间的最后4位数字 上面的代码给出了13位长的类型编号,我想要最后4位

Java 如何打印我的13位数长度中的最后4位数 最后4位数字 在这里,我试图以毫秒为单位获取当前时间的最后4位数字 上面的代码给出了13位长的类型编号,我想要最后4位,java,Java,使用“模”运算符%,如下所示: package com.nusecond.code; public class Code { public static void main(String args[]) { Long i=System.currentTimeMillis(); System.out.println(i); } } 输出:0516实际上这更接近:这是因为long不能从0开始。编辑的答案现在格式化输出。 Long

使用“模”运算符
%
,如下所示:

package com.nusecond.code;

public class Code {



    public static void main(String args[]) {

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

}

输出:
0516

实际上这更接近:这是因为long不能从
0
开始。编辑的答案现在格式化输出。
    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);
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
  }
}