在Java中,可以使用DateFormat类来格式化Date对象以获取年月日。
以下是求年月日的示例代码:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date(); // 假设已知逝去时间为当前时间
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = dateFormat.format(date);
System.out.println("年月日:" + formattedDate);
}
}
以上代码将输出当前时间的年月日,格式为yyyy-MM-dd。如果已知逝去时间不是当前时间,只需将date
对象替换为相应的Date对象即可。