java 獲取當前時間及年月日時分秒


java代碼如下:

package test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {

	public static void main(String[] args) {
		Calendar now = Calendar.getInstance();
		System.out.println("年:" + now.get(Calendar.YEAR));
		System.out.println("月:" + (now.get(Calendar.MONTH) + 1));
		System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));
		System.out.println("時:" + now.get(Calendar.HOUR_OF_DAY));
		System.out.println("分:" + now.get(Calendar.MINUTE));
		System.out.println("秒:" + now.get(Calendar.SECOND));

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("當前時間:" + sdf.format(d));
	}

}

執行結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM