在Java中獲取年份的精確度通常是指獲取當前日期時間的年份信息,包括年份的數值和精確到秒或毫秒的時間戳。以下是一些獲取年份精確度的方法:
Calendar
類獲取當前年份:Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
System.out.println("Current year: " + year);
LocalDateTime
類獲取當前年份:LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
System.out.println("Current year: " + year);
SimpleDateFormat
類獲取當前年份并精確到秒:SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date = new Date();
String year = sdf.format(date);
System.out.println("Current year: " + year);
System.currentTimeMillis()
方法獲取當前時間戳并轉換成年份:long timestamp = System.currentTimeMillis();
Date date = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String year = sdf.format(date);
System.out.println("Current year: " + year);
這些方法可以幫助您獲取當前日期時間的年份信息,并可以根據需要精確到秒或毫秒級別。