Write a java program to create experience calculator.

Write a program to read Date of Joining and the current date as Strings and Experience as integer and validate whether the given experience and calculated experience are the same. Print “true” if same, else “false”.

Input and Output Format

  • Input consists of 2 strings and an integer, where the 2 strings correspond to the date of joining and the current date, and the integer is the experience.
  • The output is either “true” or “false”.

Refer sample output for formatting specifications

Sample Input 1:

11/01/2010

01/09/2014

4

Sample Output 1:

true

Sample Input 2:

11/06/2009

01/09/2014

4

Sample Output 2:

False

Experience calculator in Java

Following are the steps to validate experience:

  • Input two strings from the user(date of joining & date of leaving) and one integer(experience).
  • Parse the string to the date format.
  • Now, calculate the time of both the string and then take the difference.
  • calculate the experience from the date of joining to the date of leaving. And compare it with the input experience.
  • If it matches then print true else print false.
package com.company;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Practice13 {
public static void main(String[] args) throws ParseException {
Date d = new Date();
Date d1 = new Date();
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
int c = sc.nextInt();
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
d = s.parse(a);
d1 = s.parse(b);
long t = d.getTime();
long t1 = d1.getTime();
long t3 = t1 - t;
long l1 = (24 * 60 * 60 * 1000);
long l = l1 * 365;
long res = t3 / l;
if (res == c) {
System.out.println("true");
} else System.out.println("false");
}
}

Output

Calculates Years of Experience in Java

Write a program in Java to find the years of experience. Let user input the date of joining as 02/12/2020 then its experience should be 1 year with the current date.

Input and Output Format

  • Input is provided in the program itself 
  • The output is a float value.

Refer sample output for formatting specifications

Sample Output 1:

1.0

Program to find years of experience in Java

Following are the steps to find the year of experience in Java:

  • First, get the current date using the LocalDate class.
  • Pass the date of joining the LocalDate class.
  • Next, use the Period class to find the period between two dates and return an integer value.
  • At last, just print the integer value.
package com.company;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.Scanner;

public class ExperienceCalculator {
    public static void main(String[] args) throws ParseException {
        LocalDate current = LocalDate.now();
        LocalDate doj = LocalDate.of(2020,12,02);
        int exp = Period.between(doj,current).getYears();
        System.out.println(exp);
    }
}

Output

Thus, this is how we can validate the experience of an employee.