1. Display Current Date and Time
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentDateTime{
public static void main(String[] args){
LocalDateTime current = LocalDateTime.now(); DateTimeFormatter
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
String formatted = current.format(formatter);
System.out.println("Current Date and Time is: \n" + formatted);
}
}
Output :-
This will print the current date and time of your place.
Current Date and Time is:
2021-04-24 19:58:00.406
2. Get IP Address
import java.net.InetAddress;
class GetMyIPAddress
{
public static void main(String args[]) throws Exception
{
InetAddress myIP=InetAddress.getLocalHost();
System.out.println("My IP Address is:");
System.out.println(myIP.getHostAddress());
}
}
Output :-
My IP Address is:
10.8.3.55
3. Check Leap Year
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int year;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
Output :-
Enter any Year: 2021
2021 is not a Leap Year.
4. Shut Down PC
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class PCShutdown {
public static void main(String[] args) throws IOException{
Runtime run = Runtime.getRuntime();
BufferedReader buffreader = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter the number of seconds after"+"Which you want your computer to shutdown");
long a = Long.parseLong(buffreader.readLine());
Process pro = run.exec("shutdown -s -t"+a);
System.exit(0);
}
}
Warning :- By running this code, your pc will be shutdown. So, for try this code make sure you clear all the running applications.
0 Comments