Calendar中就有了不同的定义: . public final static int ERA = 0;
- public final static int YEAR = 1;
- public final static int MONTH = 2;
- public final static int WEEK_OF_YEAR = 3;
- public final static int WEEK_OF_MONTH = 4;
- public final static int DATE = 5;
- public final static int DAY_OF_MONTH = 5;
- public final static int DAY_OF_YEAR = 6;
- public final static int DAY_OF_WEEK = 7;
- public final static int DAY_OF_WEEK_IN_MONTH = 8;
- public final static int AM_PM = 9;
- public final static int HOUR = 10;
- public final static int HOUR_OF_DAY = 11;
- public final static int MINUTE = 12;
- public final static int SECOND = 13;
这 些定义是不同记录方法的标识,在此没有列举完全,感兴趣的话可以看一下Calendar的源代码。 这些就是Calendar的Field,根据不同需求可以去取不同的Field,不同的记录方法也是不同的的Filed。 如,
import java.util.Calendar;public class test3 ...{
public static void main(String[] args) ...{
Calendar c = Calendar.getInstance();
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH) + 1;
int date1 = c.get(Calendar.DATE);
System.out.print("今天是" + year1 + "年" + month1 + "月" + date1 + "日");
System.out.println(" ,是今年的第" + c.get(Calendar.DAY_OF_YEAR) + "天。");
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 30);
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH) + 1;
int date2 = c.get(Calendar.DATE);
System.out.println("30天后是" + year2 + "年" + month2 + "月" + date2 + "日");
}
}
输出结果如下:
- 今天是2007年6月13日 ,是今年的第164天。
30天后是2007年7月13日
注意:int month=c.get(Calendar.MONTH)+1哦,好像系统是从0开始计月份,到了12月就归零了。所以单独取月份时,要在后面加一才能得到当前的月份。
以下是自已补充的一个小例子(2007/06/13):
import java.sql.Timestamp;import java.util.Calendar;
import java.util.Date;
public class test1 ...{
public static void main(String[] args) ...{
// 日時の取得
Calendar startCal = Calendar.getInstance();
Calendar endCal = (Calendar) startCal.clone();
Calendar tmpCal = (Calendar) startCal.clone();
long tmpTime = System.currentTimeMillis();
Timestamp time = new Timestamp(tmpTime);
Date date = new Date(time.getTime());
Calendar c = Calendar.getInstance();
c.setTime(date);
int year1 = startCal.get(Calendar.YEAR);
int month1 = startCal.get(Calendar.MONTH) + 1;
int date1 = startCal.get(Calendar.DATE);
System.out.println("今日は" + year1 + "年" + month1 + "月" + date1 + "日");
// 終了日時の取得
endCal.set(Calendar.DAY_OF_YEAR, startCal.get(Calendar.DAY_OF_YEAR) - 365);
// 日間の取得
int between = 0;
if (startCal.get(Calendar.YEAR) < endCal.get(Calendar.YEAR)) ...{
tmpCal.set(Calendar.MONTH, 11);
tmpCal.set(Calendar.DATE, 31);
for (int i = startCal.get(Calendar.YEAR); i < endCal.get(Calendar.YEAR); i++) ...{
tmpCal.set(Calendar.YEAR, i);
between = between + tmpCal.get(Calendar.DAY_OF_YEAR);
}
} else if (startCal.get(Calendar.YEAR) > endCal.get(Calendar.YEAR)) ...{
tmpCal.set(Calendar.MONTH, 11);
tmpCal.set(Calendar.DATE, 31);
for (int i = endCal.get(Calendar.YEAR); i < startCal.get(Calendar.YEAR); i++) ...{
tmpCal.set(Calendar.YEAR, i);
between = between + tmpCal.get(Calendar.DAY_OF_YEAR);
}
between = 0 - between;
}
between = between + endCal.get(Calendar.DAY_OF_YEAR) - startCal.get(Calendar.DAY_OF_YEAR);
int year2 = endCal.get(Calendar.YEAR);
int month2 = endCal.get(Calendar.MONTH) + 1;
int date2 = endCal.get(Calendar.DATE);
if (between >= 0) ...{
System.out.println(between + "日の後は" + year2 + "年" + month2 + "月" + date2 + "日");
} else ...{
System.out.println(-between + "日の前は" + year2 + "年" + month2 + "月" + date2 + "日");
}
System.out.println();
}
}
输出结果如下:
今日は2007年6月13日
365日の前は2006年6月13日
求两个日期之间的天数:
以上程序中“日間の取得”即为求两个日期之间的天数,完全通过Java原有函数计算得来,虽比较麻烦,但不必考虑程序在作加减乘除时可能存在的误差。当然也可以按如下方法来做:
long between2 = (startCal.getTimeInMillis() - endCal.getTimeInMillis())/(1000*60*60*24);
作日期的加减:
以上程序中,有通过“c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + 30);”来求得30天后的日期的,还有一种更简便的方法:
tmpCal2.add(Calendar.DAY_OF_YEAR, -30 );
取得指定年月的最后一天
同样利用Calendar类中的属性,可以取得制定年月的第一天和最后一天。以下是实现的方法:
/*** 取得指定年月的末日
* @param sDate1
* @return
*/
public static Calendar getLastDayOfMonth(Calendar sDate1) {
final int lastDay = sDate1.getActualMaximum(Calendar.DAY_OF_MONTH);
Calendar lastDate = sDate1;
lastDate.set(Calendar.DATE, lastDay);
return lastDate;
}