Javaで今日の日付を取得!年月日・時刻の取得方法まとめ
現在の日付を取得するには、JavaのLocalDateクラスを使用することができます。以下はサンプルコードです:
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 获取当前年份
int year = currentDate.getYear();
// 获取当前月份
int month = currentDate.getMonthValue();
// 获取当前日期
int day = currentDate.getDayOfMonth();
System.out.println("当前年份:" + year);
System.out.println("当前月份:" + month);
System.out.println("当前日期:" + day);
}
}
上記のコードを実行すると、現在の年、月、日が出力されます。