首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Calendar.DAy_OF_WEEK的打印,该怎么处理

2013-12-04 
Calendar.DAy_OF_WEEK的打印本帖最后由 u012432248 于 2013-12-03 12:09:51 编辑public class CalendarTes

Calendar.DAy_OF_WEEK的打印
本帖最后由 u012432248 于 2013-12-03 12:09:51 编辑

public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("   " + weekday); // 打印出来的是1 12月1日星期天
System.out.println("hjahah" + Calendar.DAY_OF_WEEK); 
// 打印出来的是7,d.set屏蔽掉直接打印Calendar.DAY_OF_WEEK也是7 为什么?不一样,这里的和
//上一句打印的不是同一内容,麻烦大神们给解释下.
}
}

[解决办法]
GregorianCalendar d = new GregorianCalendar();
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("   " + weekday);
d.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("hjahah" + d.get(Calendar.DAY_OF_WEEK));

System.out.println("hjahah" + Calendar.DAY_OF_WEEK); //这样是打印Calendar的一个常量:DAY_OF_WEEK,当然是7了。
[解决办法]
Calendar.DAY_OF_WEEK、、

DAY_OF_WEEK
public static final int DAY_OF_WEEKget 和 set 的字段数字,指示一个星期中的某天。该字段可取的值为 SUNDAY、MONDAY、TUESDAY、WEDNESDAY、THURSDAY、FRIDAY 和 SATURDAY。

楼主查一下API
[解决办法]
楼主没有看Calendar类的实现吧,下面是一段代码:
/**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * era, e.g., AD or BC in the Julian calendar. This is a calendar-specific
     * value; see subclass documentation.
     *
     * @see GregorianCalendar#AD
     * @see GregorianCalendar#BC
     */
    public final static int ERA = 0;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * year. This is a calendar-specific value; see subclass documentation.
     */
    public final static int YEAR = 1;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * month. This is a calendar-specific value. The first month of
     * the year in the Gregorian and Julian calendars is
     * <code>JANUARY</code> which is 0; the last depends on the number
     * of months in a year.
     *
     * @see #JANUARY
     * @see #FEBRUARY
     * @see #MARCH
     * @see #APRIL
     * @see #MAY
     * @see #JUNE
     * @see #JULY
     * @see #AUGUST
     * @see #SEPTEMBER
     * @see #OCTOBER
     * @see #NOVEMBER
     * @see #DECEMBER
     * @see #UNDECIMBER
     */
    public final static int MONTH = 2;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * week number within the current year.  The first week of the year, as
     * defined by <code>getFirstDayOfWeek()</code> and


     * <code>getMinimalDaysInFirstWeek()</code>, has value 1.  Subclasses define
     * the value of <code>WEEK_OF_YEAR</code> for days before the first week of
     * the year.
     *
     * @see #getFirstDayOfWeek
     * @see #getMinimalDaysInFirstWeek
     */
    public final static int WEEK_OF_YEAR = 3;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * week number within the current month.  The first week of the month, as
     * defined by <code>getFirstDayOfWeek()</code> and
     * <code>getMinimalDaysInFirstWeek()</code>, has value 1.  Subclasses define
     * the value of <code>WEEK_OF_MONTH</code> for days before the first week of
     * the month.
     *
     * @see #getFirstDayOfWeek
     * @see #getMinimalDaysInFirstWeek
     */
    public final static int WEEK_OF_MONTH = 4;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * day of the month. This is a synonym for <code>DAY_OF_MONTH</code>.
     * The first day of the month has value 1.
     *
     * @see #DAY_OF_MONTH
     */
    public final static int DATE = 5;

    /**
     * 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
     */
    public final static int DAY_OF_MONTH = 5;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * number within the current year.  The first day of the year has value 1.
     */
    public final static int DAY_OF_YEAR = 6;

    /**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * of the week.  This field takes values <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
     *
     * @see #SUNDAY
     * @see #MONDAY
     * @see #TUESDAY
     * @see #WEDNESDAY
     * @see #THURSDAY
     * @see #FRIDAY
     * @see #SATURDAY
     */
    public final static int DAY_OF_WEEK = 7;


Calendar类里面有一系列的int类型的常量,这些常量是配合get和set方法使用的一种标志,比如我去用一个Calendar的实例去get的时候,那么我们就需要传入一个标志,比如我们传DAY_OF_WEEK,是告诉jvm我们想从Calendar对象中获取对象所封装的天在星期中是第几天。如果单纯用Calendar类去调用这些常量的时候,就是返回的一个固定标识。
不知道我讲清楚了吗,希望楼主学习进步~

热点排行