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
}
}