使用Calendar類獲取當(dāng)前年月日

Calendar類是Java中處理日期和時(shí)間的重要類之一。通過Calendar類的實(shí)例,我們可以獲取當(dāng)前的年、月、日等日期信息。下面是一個(gè)示例代碼:

import java.util.Calendar;

public class GetCurrentDate {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // 月份從0開始,需要加1
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("當(dāng)前日期:" + year + "-" + month + "-" + day);
    }
}

使用Date類獲取當(dāng)前年月日

Date類是Java中用于表示日期的類。我們可以使用Date類的實(shí)例來獲取當(dāng)前的年月日。下面是一個(gè)示例代碼:

import java.util.Date;

public class GetCurrentDate {
    public static void main(String[] args) {
        Date date = new Date();
        int year = date.getYear() + 1900;
        int month = date.getMonth() + 1; // 月份從0開始,需要加1
        int day = date.getDate();

        System.out.println("當(dāng)前日期:" + year + "-" + month + "-" + day);
    }
}

使用SimpleDateFormat類獲取當(dāng)前年月日

SimpleDateFormat類是Java中用于格式化日期的類。我們可以使用SimpleDateFormat的實(shí)例來將日期按照指定的格式進(jìn)行格式化。下面是一個(gè)示例代碼:

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

public class GetCurrentDate {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(new Date());

        System.out.println("當(dāng)前日期:" + currentDate);
    }
}

使用Java 8的LocalDate類獲取當(dāng)前年月日

Java 8引入了新的日期時(shí)間API,其中LocalDate類用于表示日期。我們可以使用LocalDate類的實(shí)例獲取當(dāng)前的年月日。下面是一個(gè)示例代碼:

import java.time.LocalDate;

public class GetCurrentDate {
    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("當(dāng)前日期:" + year + "-" + month + "-" + day);
    }
}

使用Java 8的DateTimeFormatter類獲取當(dāng)前年月日

Java 8的DateTimeFormatter類用于格式化日期。我們可以使用DateTimeFormatter類的實(shí)例將日期按照指定的格式進(jìn)行格式化。下面是一個(gè)示例代碼:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class GetCurrentDate {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.now();
        String formattedDate = currentDate.format(formatter);

        System.out.println("當(dāng)前日期:" + formattedDate);
    }
}

使用System.currentTimeMillis()獲取當(dāng)前年月日

System.currentTimeMillis()方法返回從1970年1月1日00:00:00 UTC到當(dāng)前時(shí)間的毫秒數(shù)。我們可以通過將毫秒數(shù)轉(zhuǎn)換為日期來獲取當(dāng)前的年月日。下面是一個(gè)示例代碼:

import java.text.SimpleDateFormat;

public class GetCurrentDate {
    public static void main(String[] args) {
        long currentTimeMillis = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = sdf.format(currentTimeMillis);

        System.out.println("當(dāng)前日期:" + currentDate);
    }
}

使用Java 8的ZonedDateTime類獲取當(dāng)前年月日

Java 8的ZonedDateTime類用于表示帶時(shí)區(qū)的日期和時(shí)間。我們可以使用ZonedDateTime類的實(shí)例獲取當(dāng)前的年月日。下面是一個(gè)示例代碼:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class GetCurrentDate {
    public static void main(String[] args) {
        ZonedDateTime currentDate = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);

        System.out.println("當(dāng)前日期:" + formattedDate);
    }
}

總結(jié)

本文介紹了七種常用的方法來獲取Java中的當(dāng)前年月日,包括使用Calendar類、Date類、SimpleDateFormat類、Java 8的LocalDate類、DateTimeFormatter類、System.currentTimeMillis()方法以及Java 8的ZonedDateTime類。通過掌握這些方法,您可以輕松地在Java程序中獲取當(dāng)前的年月日,為日期相關(guān)的操作提供便利。