Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java中使用日历的日差异_Android_Date - Fatal编程技术网

Android java中使用日历的日差异

Android java中使用日历的日差异,android,date,Android,Date,我正在android应用程序中使用Calendar类计算日期的年中日期,然后与当前日期进行比较。以下是我使用的代码: Date now=new Date(); Calendar ca1 = Calendar.getInstance(); ca1.set(now.getYear(), now.getMonth(), now.getDate()); int nowC=ca1.get(Calendar.DAY_OF_YEAR); //Date arg0=say,get

我正在android应用程序中使用Calendar类计算日期的年中日期,然后与当前日期进行比较。以下是我使用的代码:

Date now=new Date();
Calendar ca1 = Calendar.getInstance();
        ca1.set(now.getYear(), now.getMonth(), now.getDate());
        int nowC=ca1.get(Calendar.DAY_OF_YEAR);

//Date arg0=say,get from user
        Calendar ca2 = Calendar.getInstance();
        ca2.set(arg0.GetBirthDay().getYear(),arg0.GetBirthDay().getMonth(),arg0.GetBirthDay().getDate());
        int d1C=ca2.get(Calendar.DAY_OF_YEAR);
我调试我的应用程序,并在当天看到以下值: 2012年3月2日星期五14:18:33亚洲/德黑兰

对于arg0: 1979年3月2日星期五00:00:00亚洲/德黑兰

“nowC”有62个,“d1C”有61个

我希望它们相等,因为它们都有相同的月份和日期,而且如果我使用joda包的DateTime类,如下所示,我会得到相同的结果:

int ndy=dtnow.getDayOfYear();
int d1dy=dt1.getDayOfYear(); 

为什么会这样?2012年是闰年,1979年不是。今年3月2日前还有一天-所以这两个API都给了你正确的答案


我想您可能想比较月份和日期,以获得您期望的语义。

确切地说,我想比较某人的生日和当前日期,您知道,生日的月份和日期比年份更重要