1. 程式人生 > >【JAVA】獲取系統當前時間

【JAVA】獲取系統當前時間

這裡寫圖片描述

/**
     * 可以格式化的時間
     * @return
     */
    private static String getTime1(){
        Date nowTime = new Date(); 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        String currTime = dateFormat.format( nowTime ); 
        return currTime ;
    }

    /**
     * 可以得到各部分的值
     * @return
*/
private static String getTime2(){ Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); //年 int month = c.get(Calendar.MONTH); //月 int date = c.get(Calendar.DATE); //日 int hour = c.get(Calendar.HOUR_OF_DAY);//時 int minute = c.get(Calendar.MINUTE); //分
int second = c.get(Calendar.SECOND); //秒 return year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second; }