Showing posts with label Java Programming Questions. Show all posts
Showing posts with label Java Programming Questions. Show all posts

Monday, September 7, 2026

Find Number of Days Between Two Dates in Java

Find Number of Days Between Two Given Dates in Java

Sometimes we need to find the number of days between two given dates. Earlier, I used Calendar and manually calculated the difference between the dates.

With Java 8 and later, this can be done much more easily using the java.time package.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class FindDays {

    public static void main(String[] args) {

        String str1 = "20/01/2013";
        String str2 = "28/03/2013";

        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("dd/MM/yyyy");

        LocalDate date1 = LocalDate.parse(str1, formatter);
        LocalDate date2 = LocalDate.parse(str2, formatter);

        long days = ChronoUnit.DAYS.between(date1, date2);

        System.out.println("Final Days.... " + days);
    }
}

Output:

Final Days.... 67

How it works

DateTimeFormatter is used to tell Java the format of the input dates.

LocalDate represents a date without a time or timezone.

ChronoUnit.DAYS.between() calculates the number of days between the two dates.

For example:

20/01/2013 → 28/03/2013 = 67 days

The older Calendar approach can still be found in existing Java applications, but for new development, the java.time API is generally the better choice.


Tavant Technologies Java Interview Questions - Experienced

Tavant Technologies
Round 1:F2F
1) Tell about your technical skills
2) How to work with Ajax applications?
4) Asked about page factory design concepts.
5) What are the collections u used in your project?
6) Framework explanation
7) How to find no of rows or columns in a table?
8) Diff b/w interface and abstract class?
9) Various oops concepts used in the project?
10) Roles and responsibilities.
11) SQL queries a) how to find duplicate records in a table b) Display the name of the employee who is getting         10th maximum salary
12) Logical questions:
i) A 2l bottle and a 4l bottle, By this u have to give me 3l of water? Is it possible? If yes tell me how
ii) 8 balls, having same color and weight, out of that 1 is defective, a physical balance is given, using this how to find the defective one.

Round 2
1. What is run-time polymorphism? Explain with program? Where is it achieved?
2. SQL queriesemp table fieldsename,eid,age
a) Find the name of the employee who has 3rd max age
b) Having clause and where clause
3. Logical questions
a) Cake in round shape, u have to cut the cake by 3 times only, and u have to divide into 8 equal pieces
b) Using 5 zeros, how to make it as 120
c) He gave me a paper, asked me to tore the paper once, to make 3 equal pieces


Round 3- HR+ Managerial round

1. Tell about your self
2. Why u r looking for the job?
3. What are the difficulties you faced in your previous job?
4. He is asking the same questions in different ways for 15 min
5. Framework
6. Difference b/w abstract class and interface.
7. Scenario-an abstract class implements an interface, can the abstract class implement the methods of interface?
8. From that he asked that do u perform auto it scripts in parallel execution?
9. Difference B/w primary key and unique key
10. Can unique key column hold null values? If yes how many null values?
11. Use of super keyword, order of execution
12. Collections u used in the project
13. When array will be used ? and when array list will be used?



Wednesday, July 10, 2019

Find a Missing Number from a List Using Java 8 Streams

In this article, we will find the missing number from a list of numbers using java 8.  This is one of important questions asked in interview. This program is to find the only one missing number. Check how to find all missing numbers from a list. In this program we will use only java 8 stream to find the missing number. Earlier post we had seen how to use stream to sort the employee. Check more how find one missing number using traditional core java style


FindMissingNumber.java

package com.techbyteslearn.lab.basic;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.IntStream;

public class FindMissingNumber {
public static void main(String[] args) {
ArrayList<Integer> numberList = new ArrayList<Integer>(Arrays.asList(1, 3, 2, 4, 5, 6, 7, 9, 10));
// Get the Max value from the List.
int maxValue = numberList.stream().max(Comparator.naturalOrder()).get().intValue();
// Get sum of all natural numbers - upto the above maxvalue
int sumOfAllNumber = IntStream.range(1, maxValue + 1).sum();
// Get the sum of all number inside List.
int sumofList = numberList.stream().mapToInt(Integer::intValue).sum();
// Now print the missing number.
System.out.println("The Missing Number is:: " + (sumOfAllNumber - sumofList));
}
}

Output - 

The Missing Number is:: 8