Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public int yearWorked(){

int totalWorked =
    GregorianCalendar.getInstance(YEAR) - this.hiringDate.get(Calendar.YEAR); // is this logic correct ?

return totalWorked;
}

I want to calculate the years that an employee has worked. So what i am trying is to make a simple subtraction from current date and the current employee ,but my syntax is wrong :(
Posted
Updated 28-Oct-14 7:42am
v5
Comments
Richard MacCutchan 28-Oct-14 13:44pm    
Where is the code for hiringDate.get?
voronitski.net 28-Oct-14 14:09pm    
hiringDate is just a gregorian calendar variable ,and it is instanciated with the constructor.
Richard MacCutchan 28-Oct-14 14:11pm    
See below.

1 solution

Try this:
Java
GregorianCalendar today = new GregorianCalendar();
int totalWorked = today.get(Calendar.YEAR) - hiringDate.get(Calendar.YEAR);

hiringDate must also be an instance of GregorianCalendar.
 
Share this answer
 
Comments
voronitski.net 28-Oct-14 14:15pm    
so even if when i am instaniating the hiringDate variable for example :

void main(String[] ARGS){
Employe BarackObama = new employe(2012);
}

It will not calculate like today.get(Calendar.YEAR) - "2014" ?
Richard MacCutchan 28-Oct-14 14:21pm    
Sorry but that makes no sense, you cannot subtract a string from a date.
voronitski.net 28-Oct-14 14:18pm    
ok i understand !
voronitski.net 28-Oct-14 14:18pm    
Thank you Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900