1. 程式人生 > >SimpleDateFormat得到年月日格式的字串,再轉為int型別

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