如何减去两个日期 - 一个已经宣布,一个是当前日期

时间:2018-02-18 21:52:36

标签: java arrays

我正在尝试创建一个显示员工及其雇用日期的数组,这些数据已经声明了。我需要将今天日期的设置实例变量作为退休日期,并使用它来查找他们工作的年数,月数和天数。我将项目分解为两个一起工作的文件。我只是在努力获得他们工作的岁月,月份和日子。 我的代码是第一个文件:

import java.time.*;
import java.time.LocalDate.*;
import java.time.Month.*;
import java.time.Period.*;


class Employee
{
//Instance variables
private String name;
private double salary;
private LocalDate hireDay;
private LocalDate retDay = LocalDate.now();
private int years;
private int months;
private int days;


   //Constructor - same name as class -- first thing that runs
    public Employee(String n, double s, int year, int month, int day)//, int ys, int 
    ms, int ds)
   {

    name = n;
    salary = s;
    hireDay = LocalDate.of(year, month, day);
    ///years  = ys;
   // months = ms;
    //days = ds;


}
//methods are public -- any class can have access to the method
//may or may not have a parameter
public String getName()
{
    return name;
}

public double getSalary()
{
    return salary;
}

public LocalDate getHireDay()
{
    return hireDay;
}

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

public int getRetYears()
 {
   return years;
 }
 public int getRetMonths()
 {
  return months;
 }
   public int getRetDays()
 {
 return days;
 }
  public void calcRetDay(Period timeWorked, int y, int m, int d)
 {
  timeWorked = Period.between(hireDay, retDay);
  y = timeWorked.getYears();
  m = timeWorked.getMonths();
d = timeWorked.getDays();

years = y;
months = m;  
days = d;

  }
}

我的第二个文件的代码是:

import java.time.*;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;

//*****Communicates with employee.java****************************
public class EmployeeDriver
    {
    public static void main(String[] args)
    {
     Employee[] staff = new Employee[3];
     //populate the staff array with three employee objects
     staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
     staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
     staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

     //raise everyone's salary by 5%
     for (Employee e : staff)
        //below is dot notation --- encapsulation
        e.raiseSalary(5);
     //print out information about all Employee objects
     for (Employee e : staff)
        System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay() + ",years=" + e.getRetYears()+ ",months=" + e.getRetMonths()+ ",days=" + e.getRetDays());
}
}

0 个答案:

没有答案