Java generates ten-digit timestamp (zero o'clock of the day and current time)

    /**
     * 获取当天的零点时间戳(13位)
     *
     * @return 时间戳
     */
    public static long getZeroPointDate() {
    
    
        // 获取今天零点时间戳
        long nowTime = System.currentTimeMillis();
        long dailyStartTime = nowTime - ((nowTime + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000L));
        return dailyStartTime;
    }

  public static void main(String[] args) {
    
    
 // 获取今天零点时间戳
        long nowTime = System.currentTimeMillis();
        long dailyStartTime = nowTime - ((nowTime + TimeZone.getDefault().getRawOffset()) % (24 * 60 * 60 * 1000L));
        // 方式1:十位时间戳(这种方式获取10位时间戳也可以获取,但不建议)
        String substring = String.valueOf(dailyStartTime).substring(0, 10);
        // 方式2:获取十位时间戳
        System.out.println(dailyStartTime/1000L);

//当前13位时间戳(毫秒)
System.out.println(System.currentTimeMillis());
System.out.println(new Date().getTime());
System.out.println(Calendar.getInstance().getTimeInMillis());
System.out.println(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
//当前10位时间戳(毫秒)
System.out.println(System.currentTimeMillis()/1000);
System.out.println(new Date().getTime()/1000);
System.out.println(Calendar.getInstance().getTimeInMillis()/1000);
System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
}

Guess you like

Origin blog.csdn.net/qq_40093255/article/details/115135877