Calendar cal=Calendar.getInstance();
System.out.println(cal.YEAR);//本来是2005年,为什么打出来的是"1"
System.out.println(cal.MONTH);//本来是7月,为什么打出来的是"2"
System.out.println(cal.DAY_OF_MONTH);//本来是19号,为什么打出来的是"5"
System.out.println(cal.DATE);//本来是19号,为什么打出来的是"5"
//其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期
Date date=new Date();
System.out.println(date.getYear());//本来是2005年,为什么打出来的是"105"
System.out.println(date.getMonth());//本来是7月,为什么打出来的是"6"
System.out.println(date.getDay());//本来是19号,为什么打出来的是"2"
System.out.println(date.getDate());//getDate和getDay有什么区别,得到的都是今天几号??
System.out.println(cal.YEAR);//本来是2005年,为什么打出来的是"1"
System.out.println(cal.MONTH);//本来是7月,为什么打出来的是"2"
System.out.println(cal.DAY_OF_MONTH);//本来是19号,为什么打出来的是"5"
System.out.println(cal.DATE);//本来是19号,为什么打出来的是"5"
//其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期
Date date=new Date();
System.out.println(date.getYear());//本来是2005年,为什么打出来的是"105"
System.out.println(date.getMonth());//本来是7月,为什么打出来的是"6"
System.out.println(date.getDay());//本来是19号,为什么打出来的是"2"
System.out.println(date.getDate());//getDate和getDay有什么区别,得到的都是今天几号??
Field number for get and set indicating the year. This is a calendar-specific value; date.getYear()这些方法已经不推荐使用了,可以用DateFormat来格式化date得到:
String format = "";
SimpleDateFormat formatter = new SimpleDateFormat(format);
formatter.format(date);具体格式javadoc里有讲的。比如得到当前年份,format="yyyy".
可以参考下面的例子:
GregorianCalendar cal = new GregorianCalendar();
System.out.println(""
+cal.get(GregorianCalendar.YEAR)
+cal.get(GregorianCalendar.MONTH)+1//月份是0-11
+cal.get(GregorianCalendar.DAY_OF_MONTH)
+cal.get(GregorianCalendar.HOUR_OF_DAY)
+cal.get(GregorianCalendar.MINUTE)
+cal.get(GregorianCalendar.SECOND)
);
public final static int YEAR = 1;
public final static int MONTH = 2;
public final static int DAY_OF_MONTH = 5;
public final static int DATE = 5;这是在Calendar中定义的,要不是你那里的结果才是怪事情呢,
你可以这样得到年份:System.out.println(cal.get(cal.YEAR));2、
System.out.println(date.getYear());//本来是2005年,为什么打出来的是"105"
从1900年算起: 2005-1900 = 105
System.out.println(date.getMonth());//本来是7月,为什么打出来的是"6"
月份从0开始,6表示是7月System.out.println(date.getDay());//本来是19号,为什么打出来的是"2"
今天是星期几System.out.println(date.getDate());//
这个才得到是一个月的多少号
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.YEAR));楼主应该明白了吧
Calendar cal = new GregorianCalendar();
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
System.out.println(cal.get(Calendar.DATE));
//月份必须加1
//System.out.println(cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日");
//注意和下面的比较,cal.YEAR只是作为field参数值,对于任何一天都一样
System.out.println(cal.get(cal.YEAR));
System.out.println(cal.get(cal.MONTH));
System.out.println(cal.get(cal.DAY_OF_MONTH));
System.out.println(cal.get(cal.DATE));
System.out.println(cal.YEAR);//本来是2005年,为什么打出来的是"1"
System.out.println(cal.MONTH);//本来是7月,为什么打出来的是"2"
System.out.println(cal.DAY_OF_MONTH);//本来是19号,为什么打出来的是"5"
System.out.println(cal.DATE);//本来是19号,为什么打出来的是"5"
//其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期 Date date=new Date();
System.out.println(date.getYear() + 1900);//从1900年开始计算的
System.out.println(date.getMonth()+1);//月份是从0开始算的,所以要加1
System.out.println(date.getDay());//这里是星期,相当于Calendar.get(Calendar.DAY_OF_WEEK)
System.out.println(date.getDate());//getDate和getDay有什么区别,得到的都是今天几号??
System.out.println(cal.get(cal.YEAR));//本来是2005年,为什么打出来的是"1"
System.out.println(cal.get(cal.MONTH)+1);//本来是7月,为什么打出来的是"2"
System.out.println(cal.get(cal.DAY_OF_MONTH));//本来是19号,为什么打出来的是"5"
System.out.println(cal.get(cal.DATE));//本来是19号,为什么打出来的是"5"
//其中cal.DAY_OF_MONTH和cal.DATE是不是都是日期
//cal.DATE 显示当天的是几号
//cal.DAY_OF_MONTH 显示当天在当月中是第几天
Date date=new Date();
getYear()
//Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the
local time zone.
getMonth()
//Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January.
getDay()
//Returns the day of the week represented by this date. The returned value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) represents the day of the week that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
getDate()
//Returns the day of the month represented by this Date object. The value returned is between 1 and 31 representing the day of the month that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
Calendar类中的ERA/YEAR/MONTH/WEEK_OF_YEAR/WEEK_OF_MONTH等属性,均为static int,表示的是这些属性Calendar类中实际存储数据的数组(protected int fields[])中的索引号(index),在你的程序中,可以使用
cal.get(Calendar.YEAR)
来得到年份。其它的字段的内容类似。
有一点是和其它的内容不一样的,就是MONTH,这个字段的返回值是Calendar.JANUARY到Calendar.UNDECEMBER的一个值。它的实际值是0到12这十三个值,具体这些值是什么意思请你看看DOC或SRC。
DATE和DAY_OF_MONTH的意思相同,这个在SRC中这样说了一下:
/**
* Field number for <code>get</code> and <code>set</code> indicating the
* day of the month. This is a synonym for <code>DATE</code>.
* The first day of the month has value 1.
* @see #DATE
*/Date类中有一点和Calendar类不一样,就是它的年份是从1900年记起的一个相对年份,所以今年是2005 = 1900 + 105,所以打出来的年份是105。
月份的值如上面的Calendar类。
最后的问题:Day就是星期几,今天是星期二,所以就打出来TUESDAY常量的值了,它是2所以……DATE才是这个月真正的日期。
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
在最近的一个OA中,我需要判断两个日期是否是同一周,根据一个给定的日期获得所属周的周一和周五的日期。在完成以上任务时,我发现Calendar 的确是一个功能强大的class。下面给出源码,供兄弟们参考指正。
/*
* 创建日期 2005-3-30package com.infoearth;import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @李春雷
*
public class ManageWeek {
//判断两个日期是否在同一周
boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (0 == subYear) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}
//产生周序列
public static String getSeqWeek(){
Calendar c = Calendar.getInstance(Locale.CHINA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if(week.length()==1)week = "0" + week;
String year = Integer.toString(c.get(Calendar.YEAR));
return year+week;
}
//获得周一的日期
public static String getMonday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
//获得周五的日期
public static String getFriday(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
public static void main(String[] args){ }}