SimpleDateFormat得到年月日格式的字符串,再转为int类型

1.年月日

public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
    int nowDay = Integer.parseInt(df.format(System.currentTimeMillis()).toString());

    System.out.println(nowDay);//结果20181017
}

 

2.年月日时分秒

public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
    Long nowDay = Long.parseLong(df.format(System.currentTimeMillis()).toString());

    System.out.println(nowDay);//结果20181017150714
}

3.获取时间戳,精确到秒

public static int getTimestamp() {
   int timestamp = 0;
   timestamp = (int) (System.currentTimeMillis() / 1000);
   return timestamp;
}

 

你可能感兴趣的:(java)