Examples of Get in Java

Introduction

 
In Java, we can determine various things using these examples. These examples are very important for beginners. 
 

Get Host Name in Java

 
This is used to determine the hostname in Java.
 
Example
  1. package test;  
  2. import java.net.*;  
  3. import java.io.*;  
  4. public class Test {  
  5.  public static void main(String[] args) {  
  6.   try {  
  7.    InetAddress adrs = InetAddress.getLocalHost();  
  8.    byte[] ipAdrs = adrs.getAddress();  
  9.    String hostname = adrs.getHostName();  
  10.    System.out.println("Host Name is = " + hostname);  
  11.   } catch (UnknownHostException e) {}  
  12.  }  
  13. }   
Output
 
get host name
 

Get Memory in Java

 
We can easily determine the memory size in Java using the following code.
 
Example
  1. package test;  
  2. public class Test {  
  3.  public static void main(String[] args) {  
  4.   System.out.println("Total Memory" + Runtime.getRuntime().totalMemory());  
  5.   System.out.println("Free Memory" + Runtime.getRuntime().freeMemory());  
  6.  }  
  7. }  
Output
 
get memory
 

Get Current Month in Java

 
This example will show you how to get the current month in Java.
 
Example
  1. package test;  
  2. import java.util.Calendar;  
  3. public class Test {  
  4.  public static void main(String[] args) {  
  5.   String[] months_name = {"JAN""FEB""MAR""APR""MAY""JUN",  
  6.   "JUL""AUG""SEP""OCT""NOV""DEC"  
  7.   };  
  8.   Calendar calndr = Calendar.getInstance();  
  9.   String month = months_name[calndr.get(Calendar.MONTH)];  
  10.   System.out.println("Month Name is : " + month);  
  11.  }  
  12. }  
Output
 
get month name

Get Class Name in Java

 
This example will print the class name in Java.
 
Example
  1. public class Test {  
  2.  public static void main(String args[]) {  
  3.   new Test().hi();  
  4.  }  
  5.  void hi() {  
  6.   try {  
  7.    throw new Exception("Exception");  
  8.   } catch (Exception e) {  
  9.    System.out.println(e.getStackTrace()[1].getClassName());  
  10.   }  
  11.  }  
  12. }   
Output
 
get class name
 

Get Character from String

 
In this example, we will remove characters from the string.
 
Example
  1. package test;  
  2. public class Test {  
  3.  public void StringToChar() {  
  4.   String strng = "LAPTOP";  
  5.   char[] chr_arr = strng.toCharArray();  
  6.   for (char c: chr_arr)  
  7.    System.out.println(c);  
  8.  }  
  9.  public static void main(String[] args) {  
  10.   new Test().StringToChar();  
  11.  }  
  12. }  
Output
 
get char from string

Get Current Time in Java

 
This example will give you the current time in Java.
 
Example
  1. package test;  
  2. public class Test {  
  3.  public static void main(String[] args) {  
  4.   java.util.Date current_time = new java.util.Date();  
  5.   System.out.println(new java.sql.Time(current_time.getTime()));  
  6.  }  
  7. }   
Output
 
get time

Get Difference of Two Dates

 
This example will determine the difference between two given dates in a number of days.
 
Example
  1. package test;  
  2. import java.util.*;  
  3. public class Test {  
  4.  public static void main(String args[]) {  
  5.   Test diff = new Test();  
  6.  }  
  7.  Test() {  
  8.   Calendar calndr1 = new GregorianCalendar();  
  9.   Calendar calndr2 = new GregorianCalendar();  
  10.   calndr1.set(20051212);  
  11.   calndr2.set(20131212);  
  12.   System.out.println("Days= " + Between(calndr1.getTime(), calndr2.getTime()));  
  13.  }  
  14.  public int Between(Date D1, Date D2) {  
  15.   return (int)((D2.getTime() - D1.getTime()) / (1000 * 60 * 60 * 24));  
  16.  }  
  17. }   
Output
 
difference of days
 
 
 

Summary

 
This article provided various examples using get in Java.


Similar Articles