English
Follow Us Online
Twitter    YouTube(TM)
Declare constants
Define enum
Define a method
Exit program
Exit a for loop
Exit a method
Format date
Format double
Find current directory
Get current date
Get user input
Get Class name
Java How to A B C D E F G H I J K L M N O P Q R S T U V W Y W Z
Java How to get current date
Related Links Get current date Get user input Get Class name
Steps:
  1. A new instance of the java.util.Date class gives the current date & time with milliseconds precision.
  2. A Date instance is initialized with the current day, month, year, hour, minute, seconds at the time it is created.
  3. The way date has to be displayed can be obtained by formatting using SimpleDateFormat class.
  4. Specify the pattern in which the date has to be formatted.
  5. Finally invoke the format() method on SimpleDateFormat object to format the given date.
    /**
    * Java How to get current date
    * @author www.f5java.com
    *
    **/
    
    class GetDate {
      public static void main(String args[]) {
    
        // Step 1
        Date date = new Date(); 
    
        // Steps 3 & 4
        Format f = new SimpleDateFormat("dd-MM-yyyy");
        
        // Step 5
        System.out.println("Current date is : " + f.format(date));
       
    
        // to fetch the current time, specify the hour:minutes:seconds pattern as HH:mm:ss
        f = new SimpleDateFormat("HH:mm:ss");
    
        System.out.println("Current time is : " + f.format(date));    
      }
    
    }
         
    
Java How to get user input
Steps:
  1. Create an object of java.util.Scanner class & pass System.in in the constructor to read the user input.
  2. Scanner class provides nextLine() method which reads the name from the command prompt once user has hit enter.
  3. Scanner class also provides next---() methods to read primitive values.
/**
* Java How to get user input
* @author www.f5java.com
*
**/

class GetUserInput {
   
   public static void main(String args[]) {
   
      // 1. Instantiate Scanner class passing System.in to scan from the command prompt
      Scanner sc = new Scanner(System.in);
      
      System.out.println("Name");
      
      //2. Invoke nextLine() method to read the last user input
      String name = sc.nextLine();
       
      System.out.println("Age");

      // 3. nextInt() method used to read the int value entered by the user for age
      int age = sc.nextInt();

      System.out.println("You entered ->");
      System.out.println("Name :: " + name);
      System.out.println("Age :: " + age);
   }
}
     
Java How to get Class name
Steps:
  1. Any object's class info can be obtained at runtime by getting a reference to its Class object.
  2. Invoke getClass() method on the object to get a reference to it's Class object.
  3. getName() method invoked on a Class object returns the fully qualified class name.
/**
* Java How to get class name 
* @author www.f5java.com
*
**/

class GetClassName {
  public static void main(String args[]) {

     Object ob = new Date();
     
     // 1. Invoke getClass() method on the object to get a reference to it's Class object.
     // getName() method returns the fully qualified class name of the Class object it is invoked on.
     System.out.println("Date ob's class name :: " + ob.getClass().getName());
  }
}