日历的添加方法给出了错误的值?
Add method of calendar gives wrong value?
我正在尝试使用 c.add(Calendar.DATE,1)
更改按钮点击的日期。它应该将日期增加 1。问题是当月份更改时,日期增加到 32 而不是预期的 1(一月)。月份更改为 2 月,但日期更改为 32 等等,例如32-Feb-2016
.
final TextView tv_date = (TextView) dialyReportView.findViewById(R.id.datepicker);
@Override
public void onClick(View v) {
Calendar c;
c= Calendar.getInstance();
c.add(Calendar.DATE,1);
SimpleDateFormat df= new SimpleDateFormat("DD-MMM-yyyy");
String nextDate = df.format(c.getTime());
tv_date.setText(nextDate);
}
});
我是编程新手,非常感谢您的帮助。谢谢! :)
更改您的日期格式代码行如下。
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
问题是您使用的是 D
(年中的第几天)而不是 d
(每月的第几天)。例如,下面的代码输出 31-Jan-2015
和 32-Feb-2015
:
Calendar c = Calendar.getInstance();
c.set(2015, 0, 31);
c.SimpleDateFormat df= new SimpleDateFormat("DD-MMM-yyyy");
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE,1);
System.out.println(df.format(c.getTime()));
而以下输出 31-Jan-2015
和 01-Feb-2015
:
Calendar c = Calendar.getInstance();
c.set(2015, 0, 31);
c.SimpleDateFormat df= new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE,1);
System.out.println(df.format(c.getTime()));
勾选https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
DD
是年中的第几天,dd
是月中的第几天
Letter Date or Time Component Presentation Examples
D Day in year Number 189
d Day in month Number 10
我正在尝试使用 c.add(Calendar.DATE,1)
更改按钮点击的日期。它应该将日期增加 1。问题是当月份更改时,日期增加到 32 而不是预期的 1(一月)。月份更改为 2 月,但日期更改为 32 等等,例如32-Feb-2016
.
final TextView tv_date = (TextView) dialyReportView.findViewById(R.id.datepicker);
@Override
public void onClick(View v) {
Calendar c;
c= Calendar.getInstance();
c.add(Calendar.DATE,1);
SimpleDateFormat df= new SimpleDateFormat("DD-MMM-yyyy");
String nextDate = df.format(c.getTime());
tv_date.setText(nextDate);
}
});
我是编程新手,非常感谢您的帮助。谢谢! :)
更改您的日期格式代码行如下。
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
问题是您使用的是 D
(年中的第几天)而不是 d
(每月的第几天)。例如,下面的代码输出 31-Jan-2015
和 32-Feb-2015
:
Calendar c = Calendar.getInstance();
c.set(2015, 0, 31);
c.SimpleDateFormat df= new SimpleDateFormat("DD-MMM-yyyy");
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE,1);
System.out.println(df.format(c.getTime()));
而以下输出 31-Jan-2015
和 01-Feb-2015
:
Calendar c = Calendar.getInstance();
c.set(2015, 0, 31);
c.SimpleDateFormat df= new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(df.format(c.getTime()));
c.add(Calendar.DATE,1);
System.out.println(df.format(c.getTime()));
勾选https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
DD
是年中的第几天,dd
是月中的第几天
Letter Date or Time Component Presentation Examples
D Day in year Number 189
d Day in month Number 10