For this, go to: File >> Project Structures(Ctr + Alt + Shift + s) >> Project
Click New >> JDK and provide the path and select JDK. Sometimes it may be due to other versions of JDK setup, in that case, click and select the desired version of java from dropdown menu. Then click apply and ok. That may solve the issue with java not detecting.
Sometimes, although Java is detected we are not able to use the feature of Intellij idea like, run and debug classes, step in step out from class by ctr + click on method or class, find the usage of class and method by click on it. In this case, simply close the project and import it again. For this,
Go to: File >> Close Project >> Import Project
Select your desired project, and select "create from existing source", select the desired library and java version and finish the setup. This will resolve the issues