在Java中,可以使用以下代码获取当前日期的年月日:

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

public class Main {
    public static void main(String[] args) {
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(currentDate);
        System.out.println(formattedDate);
    }
}

在上述代码中,首先创建一个Date对象表示当前时间。然后,使用SimpleDateFormat类来定义日期格式,这里使用yyyy-MM-dd表示年-月-日的格式。接下来,调用format()方法将Date对象格式化为字符串。最后,使用System.out.println()打印结果,即当前日期的年月日

java只获取当前日期年月日以Date类型得到

原文地址: http://www.cveoy.top/t/topic/hLjq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录