日期:2014-05-20  浏览次数:20746 次

java 出现时间差错 跪求求解 找错误
import java.io.*;
import java.util.*;
public class TextFileTest 
{

public static void main(String[] args)
{
Employee[] staff=new Employee[3];//定义 三个类
staff[0]=new Employee("yang",10000,1990,9,6);
staff[1]=new Employee("xu",200000,1989,9,6);
staff[2]=new Employee("xiu",30000,1989,9,6);

try
{
PrintWriter out=new PrintWriter("employee.dat");//创建一个文本文档  
writeData(staff,out);//将信息输入到文本文档中
out.close();// 关闭

Scanner in=new Scanner(new FileReader("employee.dat"));//很重要
Employee[] newStaff=readData(in);//定义三个类用来读取 文本文档的员工信息

in.close();//关闭

for(Employee e: newStaff)//通过staff
System.out.println(e); //输出所以员工的信息
}
catch(IOException exception)
{
exception.printStackTrace();

}
}



private static Employee[] readData(Scanner in)
{
int n=in.nextInt();//读取 员工 人数
in.nextLine();//读取下一行
Employee[] employee=new Employee[n];//创建n个 员工类
for(int i=0;i<n;i++)
{
employee[i]=new Employee();
employee[i].readData(in);//读取员工信息
}
return employee;//返回 员工信息信息 是数组名
}



private static void writeData(Employee[] employee, PrintWriter out) throws IOException
{
out.println(employee.length);//员工人数 int n=in.nextInt();//读取 员工 人数 紧密联系
for(Employee e:employee)
e.writeData(out);//将 信息输入到文本文档中 employee.dat
}


}
//
class Employee//公共类
{
public Employee()//默认函数
{
}

public Employee(String n,double s,int year,int month,int day)//构造函数
{
name=n;
salary=s;
GregorianCalendar calender=new GregorianCalendar(year,month-1,day);//公历
hireDay=calender.getTime(); //获取时间
}

public String getName()
{
return name;
}

public double getSalary()
{
return salary;
}

public Date getHireDay()
{
return hireDay;
}

public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}

public String toString()
{
return getClass().getName()+"[name="+name+" salary="+salary+" hireDay="+hireDay+"]";
}

public void writeData(PrintWriter out)
{
GregorianCalendar calendar=new GregorianCalendar();// 声明时间
calendar.clear();
calendar.setTime(hireDay);//创建时间
//在dat文档里面写入时间

out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+ calendar.get((Calendar.MONTH)+1) +"|"+calendar.get(Calendar.DAY_OF_WEEK));

}

public void readData(Scanner in)
{
String line=in.nextLine();//获取下一行输入
String[] tokens=line.split("\\|");//将字符串 经过"|"分割
//将员工信息经过切割分给每个 数组元素
name=tokens[0];
salary=Double.parseDouble(tokens[1]);

int y=Integer.parseInt(tokens[2]);
int m=Integer.parseInt(tokens[3]);
int d=Integer.parseInt(tokens[4]);
GregorianCalendar calendar=new GregorianCalendar(y,m-1,d);
hireDay=calendar.getTime();

}


private String name;//姓名
private double salary;//工资
private Date hireDay; //雇用日期
}










输出的结果却是

Employee[name=yang salary=10000.0 hireDay=Sat Dec 05 00:00:00 CST 1992]
Employee[name=xu salary=200000.0 hireDay=Wed Dec 04 00:00:00 CST 1991]
Employee[name=xiu salary=30000.0 hireDay=Wed Dec 04 00:00:00 CST 1991]




时间不对


为什么?





------解决方案--------------------