java - 如何获得总周数和天数

时间:2012-10-29 06:12:37

标签: java calendar

System.out.println(
  String.format(
     "You are %s weeks, and %s days pregnant", weeks , days ));

3 个答案:

答案 0 :(得分:2)

如果您正在寻找两个日期之间的天差 -

public int daysBetween(Date d1, Date d2){
 return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }

周 -

public int weeksBetween(Date d1, Date d2){
 return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24 * 7));
 }

答案 1 :(得分:0)

使用 java.util.Calendar

t.get(Calendar.DAY_OF_MONTH)     // --- int value of the day of the month (1-31)



t.get(Calendar.DAY_OF_WEEK)      // --- int value of the day of the week (0-6)

答案 2 :(得分:0)

这样做

int num ; // no. of days
int weeks ; // no of weeks
//initialise num 
weeks = num/7;
days = num % 7;
System.format("no of weeks"+weeks+"no of days"+days);