当前位置: 技术问答>java相关
如何用Calendar
来源: 互联网 发布时间:2015-11-11
本文导语: Calendar的详细用法和属性,或者那里有文档 | import java.util.Calendar; import java.util.Date; // 从Date类或其子类中得到年月日等信息 public class TCalendar { private Calendar ca=Calendar.getInstance(); private...
Calendar的详细用法和属性,或者那里有文档
|
import java.util.Calendar;
import java.util.Date;
// 从Date类或其子类中得到年月日等信息
public class TCalendar {
private Calendar ca=Calendar.getInstance();
private void setCalendar(Date today) {
ca.setTime(today);
}
public int getYear(Date today) {
this.setCalendar(today);
return ca.get(Calendar.YEAR);
}
public int getMonth(Date today) {
this.setCalendar(today);
return ca.get(Calendar.MONTH)+1;
}
public int getDate(Date today) {
this.setCalendar(today);
return ca.get(Calendar.DATE);
}
public int getHour(Date today) {
this.setCalendar(today);
return ca.get(Calendar.HOUR);
}
public int getMinute(Date today) {
this.setCalendar(today);
return ca.get(Calendar.MINUTE);
}
public int getSecond(Date today) {
this.setCalendar(today);
return ca.get(Calendar.SECOND);
}
/*
public static void main(String str[]) {
Date d=new Date(System.currentTimeMillis());
System.out.println("d: "+d.getMonth());
TClendar tc=new TClendar();
System.out.println(tc.getMonth(d));
}
*/
}
import java.util.Date;
// 从Date类或其子类中得到年月日等信息
public class TCalendar {
private Calendar ca=Calendar.getInstance();
private void setCalendar(Date today) {
ca.setTime(today);
}
public int getYear(Date today) {
this.setCalendar(today);
return ca.get(Calendar.YEAR);
}
public int getMonth(Date today) {
this.setCalendar(today);
return ca.get(Calendar.MONTH)+1;
}
public int getDate(Date today) {
this.setCalendar(today);
return ca.get(Calendar.DATE);
}
public int getHour(Date today) {
this.setCalendar(today);
return ca.get(Calendar.HOUR);
}
public int getMinute(Date today) {
this.setCalendar(today);
return ca.get(Calendar.MINUTE);
}
public int getSecond(Date today) {
this.setCalendar(today);
return ca.get(Calendar.SECOND);
}
/*
public static void main(String str[]) {
Date d=new Date(System.currentTimeMillis());
System.out.println("d: "+d.getMonth());
TClendar tc=new TClendar();
System.out.println(tc.getMonth(d));
}
*/
}
|
Field Summary
static int AM
Value of the AM_PM field indicating the period of the day from midnight to just before noon.
static int AM_PM
Field number for get and set indicating whether the HOUR is before or after noon.
static int APRIL
Value of the MONTH field indicating the fourth month of the year.
protected boolean areFieldsSet
True if fields[] are in sync with the currently set time.
static int AUGUST
Value of the MONTH field indicating the eighth month of the year.
static int DATE
Field number for get and set indicating the day of the month.
static int DAY_OF_MONTH
Field number for get and set indicating the day of the month.
static int DAY_OF_WEEK
Field number for get and set indicating the day of the week.
static int DAY_OF_WEEK_IN_MONTH
Field number for get and set indicating the ordinal number of the day of the week within the current month.
static int DAY_OF_YEAR
Field number for get and set indicating the day number within the current year.
static int DECEMBER
Value of the MONTH field indicating the twelfth month of the year.
static int DST_OFFSET
Field number for get and set indicating the daylight savings offset in milliseconds.
static int ERA
Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
static int FEBRUARY
Value of the MONTH field indicating the second month of the year.
static int FIELD_COUNT
The number of distict fields recognized by get and set.
protected int[] fields
The field values for the currently set time for this calendar.
static int FRIDAY
Value of the DAY_OF_WEEK field indicating Friday.
static int HOUR
Field number for get and set indicating the hour of the morning or afternoon.
static int HOUR_OF_DAY
Field number for get and set indicating the hour of the day.
protected boolean[] isSet
The flags which tell if a specified time field for the calendar is set.
protected boolean isTimeSet
True if then the value of time is valid.
static int JANUARY
Value of the MONTH field indicating the first month of the year.
static int JULY
Value of the MONTH field indicating the seventh month of the year.
static int JUNE
Value of the MONTH field indicating the sixth month of the year.
static int MARCH
Value of the MONTH field indicating the third month of the year.
static int MAY
Value of the MONTH field indicating the fifth month of the year.
static int MILLISECOND
Field number for get and set indicating the millisecond within the second.
static int MINUTE
Field number for get and set indicating the minute within the hour.
static int MONDAY
Value of the DAY_OF_WEEK field indicating Monday.
static int MONTH
Field number for get and set indicating the month.
static int NOVEMBER
Value of the MONTH field indicating the eleventh month of the year.
static int OCTOBER
Value of the MONTH field indicating the tenth month of the year.
static int PM
Value of the AM_PM field indicating the period of the day from noon to just before midnight.
static int SATURDAY
Value of the DAY_OF_WEEK field indicating Saturday.
static int SECOND
Field number for get and set indicating the second within the minute.
static int SEPTEMBER
Value of the MONTH field indicating the ninth month of the year.
static int SUNDAY
Value of the DAY_OF_WEEK field indicating Sunday.
static int THURSDAY
Value of the DAY_OF_WEEK field indicating Thursday.
protected long time
The currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.
static int TUESDAY
Value of the DAY_OF_WEEK field indicating Tuesday.
static int UNDECIMBER
Value of the MONTH field indicating the thirteenth month of the year.
static int WEDNESDAY
Value of the DAY_OF_WEEK field indicating Wednesday.
static int WEEK_OF_MONTH
Field number for get and set indicating the week number within the current month.
static int WEEK_OF_YEAR
Field number for get and set indicating the week number within the current year.
static int YEAR
Field number for get and set indicating the year.
static int ZONE_OFFSET
Field number for get and set indicating the raw offset from GMT in milliseconds.
static int AM
Value of the AM_PM field indicating the period of the day from midnight to just before noon.
static int AM_PM
Field number for get and set indicating whether the HOUR is before or after noon.
static int APRIL
Value of the MONTH field indicating the fourth month of the year.
protected boolean areFieldsSet
True if fields[] are in sync with the currently set time.
static int AUGUST
Value of the MONTH field indicating the eighth month of the year.
static int DATE
Field number for get and set indicating the day of the month.
static int DAY_OF_MONTH
Field number for get and set indicating the day of the month.
static int DAY_OF_WEEK
Field number for get and set indicating the day of the week.
static int DAY_OF_WEEK_IN_MONTH
Field number for get and set indicating the ordinal number of the day of the week within the current month.
static int DAY_OF_YEAR
Field number for get and set indicating the day number within the current year.
static int DECEMBER
Value of the MONTH field indicating the twelfth month of the year.
static int DST_OFFSET
Field number for get and set indicating the daylight savings offset in milliseconds.
static int ERA
Field number for get and set indicating the era, e.g., AD or BC in the Julian calendar.
static int FEBRUARY
Value of the MONTH field indicating the second month of the year.
static int FIELD_COUNT
The number of distict fields recognized by get and set.
protected int[] fields
The field values for the currently set time for this calendar.
static int FRIDAY
Value of the DAY_OF_WEEK field indicating Friday.
static int HOUR
Field number for get and set indicating the hour of the morning or afternoon.
static int HOUR_OF_DAY
Field number for get and set indicating the hour of the day.
protected boolean[] isSet
The flags which tell if a specified time field for the calendar is set.
protected boolean isTimeSet
True if then the value of time is valid.
static int JANUARY
Value of the MONTH field indicating the first month of the year.
static int JULY
Value of the MONTH field indicating the seventh month of the year.
static int JUNE
Value of the MONTH field indicating the sixth month of the year.
static int MARCH
Value of the MONTH field indicating the third month of the year.
static int MAY
Value of the MONTH field indicating the fifth month of the year.
static int MILLISECOND
Field number for get and set indicating the millisecond within the second.
static int MINUTE
Field number for get and set indicating the minute within the hour.
static int MONDAY
Value of the DAY_OF_WEEK field indicating Monday.
static int MONTH
Field number for get and set indicating the month.
static int NOVEMBER
Value of the MONTH field indicating the eleventh month of the year.
static int OCTOBER
Value of the MONTH field indicating the tenth month of the year.
static int PM
Value of the AM_PM field indicating the period of the day from noon to just before midnight.
static int SATURDAY
Value of the DAY_OF_WEEK field indicating Saturday.
static int SECOND
Field number for get and set indicating the second within the minute.
static int SEPTEMBER
Value of the MONTH field indicating the ninth month of the year.
static int SUNDAY
Value of the DAY_OF_WEEK field indicating Sunday.
static int THURSDAY
Value of the DAY_OF_WEEK field indicating Thursday.
protected long time
The currently set time for this calendar, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.
static int TUESDAY
Value of the DAY_OF_WEEK field indicating Tuesday.
static int UNDECIMBER
Value of the MONTH field indicating the thirteenth month of the year.
static int WEDNESDAY
Value of the DAY_OF_WEEK field indicating Wednesday.
static int WEEK_OF_MONTH
Field number for get and set indicating the week number within the current month.
static int WEEK_OF_YEAR
Field number for get and set indicating the week number within the current year.
static int YEAR
Field number for get and set indicating the year.
static int ZONE_OFFSET
Field number for get and set indicating the raw offset from GMT in milliseconds.
|
java doc 里面讲得非常详细也有例子,快去看吧
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。