android-MonthDisplayHelper.NumberOfDaysInMonth在MonoDroid中返回不正确的值

提问

我想创建一个Calendar,但是MonthDisplayHelper返回的值不正确(2013年2月为31,即28天).我究竟做错了什么?

DateTime mRightNow = DateTime.Now;
MonthDisplayHelper mHelper = new MonthDisplayHelper(mRightNow.Year, mRightNow.Month, 2);
Log.Info("cal", mHelper.NumberOfDaysInMonth);

最佳答案

您正在混合.Net和Java DateTime类!

.Net月基于1

Java月份从0开始

所以DateTime.Now是.Net并在第二个月(2月)返回某天

但是MonthDisplayHelper是Java语言,因此将2解释为第三个月(3月)

通过使用mRightNow-1修复此问题

也许考虑使用扩展方法使其更具可读性

public static class JavaDateTimeExtensions
{
    public static int JavaMonth(this DateTime input)
    {
        return input.Month - 1;
    }
}