Sunday, 5 July 2015

Time in seconds to execute scripts, IP Address and environment detials - JAVA

package test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;



public class SystemEg {

    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("os.arch"));
       
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostName();
        System.out.println("Hostname: "+hostname);
        System.out.println("IP Address: "+addr.getHostAddress());
        System.out.println("Local Host: "+addr.getLocalHost());
       
       
        Date todaysDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss a");
        String formattedDate = formatter.format(todaysDate);
        System.out.println(formattedDate);
       
//        Calculate execution time in seconds..
        long iStartTime = 0;
        long iEndTime = 0;
        iStartTime = System.currentTimeMillis();
        Thread.sleep(3000);
        iEndTime = System.currentTimeMillis();   
        System.out.println(TimeUnit.MILLISECONDS.toSeconds(iEndTime-iStartTime));       
    }
}

/*
Output:
Windows 7
6.1
amd64
Hostname: IN22222
IP Address: xxx.xxx.x.x
12:23:46 PM
3
*/

No comments:

Post a Comment