好的,我想让我的程序打印出日期: 1/1/2009
但这是它打印出来的:
2009年东部标准时间清华1月01 00:00:00
从这段代码
GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
public void setStart()
{
startDate.setLenient(false);
Date date = new Date(startDate.getTimeInMillis());
System.out.println(date);
}如何才能将其更改为仅打印2009年1月1日?
发布于 2009-09-13 02:35:56
使用SimpleDateFormat
GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
public void setStart() {
startDate.setLenient(false);
DateFormat df = new SimpleDateFormat("d/M/yyyy");
df.format(startDate.getDate());
}您隐式地调用了toString()方法,该方法(正确地)打印出完整的内容。
顺便说一句,没有必要像你现在这样构造一个日期。在Calendar上调用getDate()将返回一个Date对象。
https://stackoverflow.com/questions/1416682
复制相似问题