Topic: 关于GregorianCalendar类的DAY_OF_MONTH问题 |
Print this page |
1.关于GregorianCalendar类的DAY_OF_MONTH问题 | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-08 15:13 我是菜鸟 最近刚开始看 CORE JAVA这本书 其中有一段关于GregorianCalendar类的例程 我稍微改了一下以对其方法和属性有进一步了解 代码如下: 文件名:gCalendar.java ================================== import java.util.*; public class gCalendar { public static void main(String[] args) { GregorianCalendar g_now = new GregorianCalendar(2005,6,1); Date d_now = g_now.getTime(); System.out.println(g_now); System.out.println(d_now); System.out.println("DAY_OF_MONTH = ["+g_now.DAY_OF_MONTH+"]"); } } ================================== 输出结果如下: ================================== java.util.GregorianCalendar[time=1120147200000,areFieldsSet=false,areAllFieldsSe t=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=2 8800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfW eek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2005,MONTH=6,WEEK_OF_YEAR=?,WEEK_OF_MO NTH=?,DAY_OF_MONTH=1,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=?, HOUR=?,HOUR_OF_DAY=?,MINUTE=?,SECOND=?,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?] Fri Jul 01 00:00:00 CST 2005 DAY_OF_MONTH = [5] =================================== 问题是 日期设为2005年7月1日,为什么DAY_OF_MONTH的值是5呢?从直接输出的GregorianCalendar数据看 其中的DAY_OF_MONTH明明是1呀? |
2.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: maple Posted on: 2005-07-08 17:31 DAY_OF_MONTH是GregorianCalendar这个类中的一个public static的一个常量,是取得DAY_OF_MONTH这个值的ID,想要取得这个值需要g_now.get(GregorianCalendar.DAY_OF_MONTH)这么来获得。 |
3.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-08 21:02 YES!谢谢楼上的兄弟! |
4.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-08 21:03 那象我这样的写法 取出来的是什么东东呢? |
5.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: why Posted on: 2005-07-08 23:15 szaba wrote: g_now.toString() d_now.toString() |
6.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: why] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-09 06:58 老大 不好意思 我还是不明白 能说详细一点吗? |
7.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: why Posted on: 2005-07-09 07:55 szaba wrote: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html out public static final PrintStream outThe "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user. For simple stand-alone Java applications, a typical way to write a line of output data is: System.out.println(data) See the println methods in class PrintStream. http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#print(java.lang.Object) public void print(Object obj) Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method. Parameters: obj - The Object to be printed See Also: Object.toString() |
8.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: why] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-09 22:29 why wrote: 可是 如果我把您说的g_now.toString()直接用println输出的话 内容是下面这样子 DAY_of_MONTH = [java.util.GregorianCalendar[time=1120919176762,areFieldsSet=true ,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shan ghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=nul l],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2005,MONTH=6,WEEK_OF_YEA R=28,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=190,DAY_OF_WEEK=7,DAY_OF_WEEK_IN _MONTH=2,AM_PM=1,HOUR=10,HOUR_OF_DAY=22,MINUTE=26,SECOND=16,MILLISECOND=762,ZONE _OFFSET=28800000,DST_OFFSET=0]] 这个结果和我原来写的大相径庭啊 原来的是这样的 DAY_of_MONTH = [5] DAY_of_YEAR = [6] 这个 5 和6具体是怎么出来的呢? 请WHY老大继续赐教!谢谢! |
9.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: why Posted on: 2005-07-10 00:48 szaba wrote: I don't know what you've done and how you got the above result. You should not access g_now.DAY_OF_MONTH and g_now.DAY_OF_YEAR, which are static constants (field numbers) defined for java.util.Calendar http://java.sun.com/j2se/1.4.2/docs/api/constant-values.html#java.util.Calendar.DAY_OF_MONTH http://java.sun.com/j2se/1.4.2/docs/api/constant-values.html#java.util.Calendar.DAY_OF_YEAR public static final int DAY_OF_MONTH 5 public static final int DAY_OF_WEEK 7 public static final int DAY_OF_WEEK_IN_MONTH 8 public static final int DAY_OF_YEAR 6 |
10.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: bluepure Posted on: 2005-07-10 10:07 源代码定义的全局静态不可变变量就是 public final static int DAY_OF_MONTH = 5; 你真要问为什么,就去找sun吧。 真正的取时间值是调用get方法: g_now.get(g_now.DAY_OF_MONTH) System.out.println("DAY_OF_MONTH = [" + g_now.get(g_now.DAY_OF_MONTH) + "]"); 这个才是取得域为 DAY_OF_MONTH 的值。 |
11.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-10 11:08 呵呵 楼上两位老大 多谢了! 我是菜鸟 还没敢想去看DOC “源代码定义的全局静态不可变变量就是” 明白了! 再次感谢各位!!! |
12.Re:关于GregorianCalendar类的DAY_OF_MONTH问题 [Re: szaba] | Copy to clipboard |
Posted by: szaba Posted on: 2005-07-11 15:11 了解了 既然是常量 那么记住了以后只要这样写结果也是一样的 g_now.get(5) 跟 g_now.get(GregorianCalendar.DAY_OF_MONTH) 是一样的 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |