Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to get name of current executed method and list of methods executing on the stack of main Thread in Java?

ADHREQQGA7TT

Program to demonstrate how to get name of current executed method and list of methods executing on the stack of main Thread in Java.


package com.hubberspot.example;


public class CurrentExecutedMethodName {

 public static void main(String[] args) {
  System.out.println("In method : main()");

  // Getting the Current Thread executing main method
  Thread thread = Thread.currentThread();
  // Getting an array of elements on stack trace of thread
  StackTraceElement[] stackTraceElements = thread.getStackTrace();

  // Using the getMethodName() of StackTraceElement
  // We obtain the executing method on the stack
  System.out.println("Method name is : "+ 
    stackTraceElements[1].getMethodName());

  // Adding yet another method on the stack 
  methodA();
 }

 private static void methodA() {
  System.out.println("In method : methodA()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();

  System.out.println("Method name is : "+ 
    stackTraceElements[1].getMethodName());

  // Adding yet another method on the stack
  methodB();
 }

 private static void methodB() {
  System.out.println("In method : methodB()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();

  System.out.println("Method name is : "+ 
    stackTraceElements[1].getMethodName());

  // Adding yet another method on the stack
  methodC();
 }

 private static void methodC() {
  System.out.println("In method : methodC()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();

  System.out.println("Method name is : "+ 
    stackTraceElements[1].getMethodName());

  // Adding yet another method on the stack
  // This method will print list of all methods
  // on the stack trace waiting for the execution
  completeStackTrace();

 }

 private static void completeStackTrace() {
  System.out.println("In method : completeStackTrace()");
  Thread thread = Thread.currentThread();
  StackTraceElement[] stackTraceElements = thread.getStackTrace();

  System.out.println();
  System.out.println("Printing Complete Stack Trace ... \n");

  // Looping the StackTraceElements and printing 
  // the methods of it 
  for(int i = 0; i< stackTraceElements.length; i++) {
   System.out.println("Method at "+ i +" : "+ 
     stackTraceElements[i].getMethodName());
  }

 }

}


Output of the program : 


 

ADHREQQGA7TT

How to add and subtract hour to current hours using Calendar class in Java ?.

Program to demonstrate addition and subtraction of hours to current hour of the day using Calendar class in Java


package com.hubberspot.example;

import java.util.Calendar;


public class TimeManipulation {

 public static void main(String[] args) {

  // Create a Calendar object
  Calendar calendar = Calendar.getInstance();

  // Get current day from calendar
  int day = calendar.get(Calendar.DATE);
  // Get current month from calendar
  int month = calendar.get(Calendar.MONTH) + 1;  
  // Get current year from calendar
  int year = calendar.get(Calendar.YEAR);

  System.out.print("Today's Date : ");
  // printing todays date in dd/mm/yy
  System.out.println(day+"/"+month+"/"+year);

  // Get current hour from calendar
  int hour = calendar.get(Calendar.HOUR_OF_DAY);
  // Get current minute from calendar
  int minutes = calendar.get(Calendar.MINUTE);
  // Get current second from calendar
  int seconds = calendar.get(Calendar.SECOND);

  System.out.print("Current Time : ");
  // printing current time in hh:mm:ss
  System.out.println(hour+":"+minutes+":"+seconds);

  // adding 6 hours to current hour of the day
  calendar.add(Calendar.HOUR_OF_DAY, 6);

  hour = calendar.get(Calendar.HOUR_OF_DAY);
  minutes = calendar.get(Calendar.MINUTE);
  seconds = calendar.get(Calendar.SECOND);

  System.out.print("Time after 6 hours : ");
  // printing time after 6 hours
  System.out.println(hour+":"+minutes+":"+seconds);

  // Calendar class automatically adjust date if 
  // on adding hours makes a date change
  day = calendar.get(Calendar.DATE);
  month = calendar.get(Calendar.MONTH) + 1;  
  year = calendar.get(Calendar.YEAR);

  System.out.print("Date after 6 hours : ");
  // printing date after 6 hours in dd/mm/yy
  System.out.println(day+"/"+month+"/"+year);

  // as our current hour is set to 6 hours plus from
  // the current hour , in order to subtract 6 
  // hours from current hour minus it with 12 
  calendar.add(Calendar.HOUR_OF_DAY, -12);

  hour = calendar.get(Calendar.HOUR_OF_DAY);
  minutes = calendar.get(Calendar.MINUTE);
  seconds = calendar.get(Calendar.SECOND);

  System.out.print("Time before 6 hours : ");
  // printing time after subtraction in hh:mm:ss
  System.out.println(hour+":"+minutes+":"+seconds);

 }

}




Output of the program :



How to use aliases when referring to a bean in configuration file of Spring Framework ?.

A simple example to demonstrate, how to use Aliases when referring to a bean configuration file in Spring Framework

1. Create a Dog class

package com.hubberspot.spring;


public class Dog {

 private String tail;
 private String coat;
 private String breed;

 public String getTail() {
  return tail;
 }

 public void setTail(String tail) {
  this.tail = tail;
 }

 public String getCoat() {
  return coat;
 }

 public void setCoat(String coat) {
  this.coat = coat;
 }

 public String getBreed() {
  return breed;
 }

 public void setBreed(String breed) {
  this.breed = breed;
 }

 public void move() {
  System.out.println("A Dog of breed "+ getBreed() +", " +
    "having "+ getCoat() +"\nand "+ getTail() + " " +
    "tail moves slowly ...");
 }


}




2. Create a Spring configuration file say spring.xml and place it in classpath i.e src folder of your Eclipse



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 <!-- In order to create an object of bean we define its properties in bean 
  tag. The 'id' attribute value can be thought of as a reference to 'class' 
  attribute value -->

 <bean id="dog" class="com.hubberspot.spring.Dog">


  <!-- setting the property of dog from the spring configuration file, the 
   property tag uses the setter method to set the properties to its respective 
   field -->

  <property name="tail" value="straight"></property>
  <property name="coat" value="stripes on white fur"></property>
  <property name="breed" value="German Shepherd"></property>

 </bean>

 <!-- here name is mapped with the id of the bean and alias is the new name 
  designers provide as per requirement -->
 <alias name="dog" alias="dog-alias" />


</beans>



3. Create a test class and run to see the output below :

package com.hubberspot.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class WildLifeApplication {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Dog object. Without implementing new keyword we 
  // have injected object of Dog just by reading an xml 
  // configuration file.
  Dog dog = (Dog)context.getBean("dog");

  // Calling our functionality
  dog.move();
  
  // getting the same bean of dog by calling the alias of dog
  // alias help us by providing different name to a single
  // bean as per our design needs.
  // check for changes done in spring.xml file
  dog = (Dog)context.getBean("dog-alias");

  // Calling our functionality
  dog.move();

 }

}





Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot