Java中的长数学

Java中的长数学,java,date,math,long-integer,Java,Date,Math,Long Integer,我在java的一些长数学方面有点困难。 我想做的是以周为单位显示一个日期,这样我可以从两周开始计算时间。我看过一些教程,但似乎没有一本有效 下面是问题的代码 public void onClick(View v) { //for the placement date final Calendar cal = Calendar.getInstance(); year=cal.get(Calendar.YEAR); month=cal.get(Calendar.MO

我在java的一些长数学方面有点困难。 我想做的是以周为单位显示一个日期,这样我可以从两周开始计算时间。我看过一些教程,但似乎没有一本有效

下面是问题的代码

public void onClick(View v) 
 { //for the placement date
     final Calendar cal = Calendar.getInstance();
     year=cal.get(Calendar.YEAR);
     month=cal.get(Calendar.MONTH);
     day=cal.get(Calendar.DAY_OF_MONTH);
     showDialog(START_DATE_DIALOG_ID);


     placementDate = new Long(cal.getTimeInMillis());
     placementDate /=604800000L; //takes the date in ms to weeks
 }
我使用两个日期执行此函数两次,因此当以ms格式输出时,我得到两个不同的数字:

  • 2014年4月11日-1397217773259
  • 2014年5月12日-1397217815087
当除以时,我得到了完全相同的值,都是2310


目前我尝试了两种格式来计算日期,第一种是代码中显示的日期除以86400000,第二种是除以(1000*60*60*24*7),每种方式的输出结果是2310。我只是不明白,或者似乎无法找到方法让它按照它应该的方式工作

如果您想要周数,请使用以下内容:

cal.get(Calendar.WEEK_OF_YEAR);

如果需要周数,请使用以下选项:

cal.get(Calendar.WEEK_OF_YEAR);

如果需要周数,请使用以下选项:

cal.get(Calendar.WEEK_OF_YEAR);

如果需要周数,请使用以下选项:

cal.get(Calendar.WEEK_OF_YEAR);

您显示的数字1397217773259和1397217815087之间的差值为41828或小于42秒。这显然比你的除数604800000小得多


检查您的
日历逻辑。

您显示的数字1397217773259和1397217815087之间的差值为41828或小于42秒。这显然比你的除数604800000小得多


检查您的
日历逻辑。

您显示的数字1397217773259和1397217815087之间的差值为41828或小于42秒。这显然比你的除数604800000小得多


检查您的
日历逻辑。

您显示的数字1397217773259和1397217815087之间的差值为41828或小于42秒。这显然比你的除数604800000小得多

检查您的
日历
逻辑。

简单数学 年=52周,大概是因为它是52.12 52*本年度+本年度周 就这样

public static void main(String[] args) {
     Calendar cal = Calendar.getInstance();
     double year =cal.get(Calendar.YEAR);
     double month=cal.get(Calendar.MONTH);
     double weekOfYear = Calendar.WEEK_OF_YEAR;
     double currentYearWeeks = (year-1)*52 + weekOfYear;
     double weekAgo = (year-1)*52 + weekOfYear -1;
     double monthAgo = (year-1)*52 + weekOfYear - 4;
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - monthAgo));
}
简单数学 年=52周,大概是因为它是52.12 52*本年度+本年度周 就这样

public static void main(String[] args) {
     Calendar cal = Calendar.getInstance();
     double year =cal.get(Calendar.YEAR);
     double month=cal.get(Calendar.MONTH);
     double weekOfYear = Calendar.WEEK_OF_YEAR;
     double currentYearWeeks = (year-1)*52 + weekOfYear;
     double weekAgo = (year-1)*52 + weekOfYear -1;
     double monthAgo = (year-1)*52 + weekOfYear - 4;
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - monthAgo));
}
简单数学 年=52周,大概是因为它是52.12 52*本年度+本年度周 就这样

public static void main(String[] args) {
     Calendar cal = Calendar.getInstance();
     double year =cal.get(Calendar.YEAR);
     double month=cal.get(Calendar.MONTH);
     double weekOfYear = Calendar.WEEK_OF_YEAR;
     double currentYearWeeks = (year-1)*52 + weekOfYear;
     double weekAgo = (year-1)*52 + weekOfYear -1;
     double monthAgo = (year-1)*52 + weekOfYear - 4;
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - monthAgo));
}
简单数学 年=52周,大概是因为它是52.12 52*本年度+本年度周 就这样

public static void main(String[] args) {
     Calendar cal = Calendar.getInstance();
     double year =cal.get(Calendar.YEAR);
     double month=cal.get(Calendar.MONTH);
     double weekOfYear = Calendar.WEEK_OF_YEAR;
     double currentYearWeeks = (year-1)*52 + weekOfYear;
     double weekAgo = (year-1)*52 + weekOfYear -1;
     double monthAgo = (year-1)*52 + weekOfYear - 4;
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - weekAgo));
     System.out.println("Difference:" + (currentYearWeeks - monthAgo));
}

长数学没有什么错。事实上,两者的正确答案都是2310。差值只有0.000069,四舍五入。长的数学没有问题。事实上,两者的正确答案都是2310。差值只有0.000069,四舍五入。长的数学没有问题。事实上,两者的正确答案都是2310。差值只有0.000069,四舍五入。长的数学没有问题。事实上,两者的正确答案都是2310。差值仅为0.000069,四舍五入。