Topic: 问一个Date类的奇怪问题

  Print this page

1.问一个Date类的奇怪问题 Copy to clipboard
Posted by: hahaer1234
Posted on: 2005-04-13 15:24

小弟java初学,今天碰到一个不解的问题,向各位大虾请教,请不吝赐教
我在用Date类的时候,遇到这样一种现象,当我使用GetDay()方法的时候,如果调用方法的对象是系统默认时间的对象,得出来的星期几结果是正确的,如果是我指定时间定义的对象调用,则得出的星期几的结果不正确,请问这是怎么回事?是我的语法写的不对,还是和系统的配置或者OS有关呢?
谢谢

所写代码如下:

import java.util.*;

public class DayTest
{
  public static void main(String[] args)
  {
    Date date1=new Date();
    Date date2=new Date(2005,4,13);
    int week1=date1.getDay();
    int week2=date2.getDay();
    System.out.println("The Time1 is:"+(date1.getYear()+1900)+(date1.getMonth()+1)+date1.getDate()+
      " The Week1 is:"+week1);
    System.out.println("The Time2 is:"+date2.getYear()+date2.getMonth()+date2.getDate()+
      " The Week2 is:"+week2);
  }
}

结果如下:
The Time1 is 2005413 The Week1 is:3
The Time2 is 2005413 The Week2 is:6

2.Re:问一个Date类的奇怪问题 [Re: hahaer1234] Copy to clipboard
Posted by: cxp108
Posted on: 2005-04-13 18:15

不要使用Date(int,int,int)这个构造函数,而使用Date(long)这个构造函数,
如果算日期的话,一定要用setTime(long time) time 参数可以从当前日期
加或减获得

3.Re:问一个Date类的奇怪问题 [Re: hahaer1234] Copy to clipboard
Posted by: why
Posted on: 2005-04-13 20:37

是你的语法写的不对

new Date(2005,4,13); actually refers to (2005+1900)-(4+1)-13, i.e. 3905-5-13
sigh Sad


http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(int, int, int, int, int)

Date
public Date(int year,
int month,
int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments.

Parameters:
year - the year minus 1900.
month - the month between 0-11.
date - the day of the month between 1-31.

4.Re:问一个Date类的奇怪问题 [Re: hahaer1234] Copy to clipboard
Posted by: hahaer1234
Posted on: 2005-04-14 07:58

谢谢两位大佬,小弟明白啦


   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