Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Friday, 13 February 2015

Date Calculation : Java to get date before or after some days from today

Calendar cNow = Calendar.getInstance();
Date dNow = cNow.getTime();          
cNow.add(Calendar.DATE, Day);           
Date beforeDate = cNow.getTime();           
SimpleDateFormat format = 
         new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
           
String dateNow = format.format(dNow);          
String dayBefore = format.format(beforeDate);          

System.out.println(dateNow);         
System.out.println(dayBefore);

________________________________________________
Day is a integer value either in + or - base on requirement

- Day will give you past date

+ Day will give you future date   

Thursday, 12 February 2015

Call Rest service from JAVA Core, Post method

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class SubscriberService {

       public void sendPost() throws Exception {

              try {
                      DefaultHttpClient httpClient = new DefaultHttpClient();
                      HttpPost postRequest = new HttpPost("api url");

                      StringEntity input = new StringEntity(
                                    "{"
                                    + "\"object\":\"value\""
                                    + "}");
                      input.setContentType("application/json");
                      postRequest.setEntity(input);
                      HttpResponse response = httpClient.execute(postRequest);
                      BufferedReader br = new BufferedReader(new InputStreamReader(
                                    (response.getEntity().getContent())));

                      String output;
                      System.out.println("Output from Server .... \n");
                      while ((output = br.readLine()) != null) {
                             System.out.println(output);
                      }
                      httpClient.getConnectionManager().shutdown();
              } catch (MalformedURLException e) {
                      e.printStackTrace();
              } catch (IOException e) {
                      e.printStackTrace();
              }
       }

}

Thursday, 4 September 2014

JAVA make Class non Inheritable

final : key word is use for make class as Final with only one reason no other class can inherit Final Class Eg:
______________________________________________________

package com.mastermoin.ICore;

public final class FinalClass {

       public void methodFromFinalClass(){
      
       }
}
______________________________________________________________________________


package com.mastermoin.java;

import com.mastermoin.ICore.FinalClass;

public class DemoRun extends FinalClass{

       public static void main(String[] args) {

       }

}
__________________________________________________________________
Output 

Compile Time Error : The type DemoRun cannot subclass the final class FinalClass

JAVA Use Interface with Abstract Class

package com.mastermoin.ICore;

public interface InterfaceOne {
       void methodFromInterfaceOne();
}

________________________________________________________________________

package com.mastermoin.ICore;

public interface InterfaceTwo {
       void methodFromInterfaceTwo();
}
________________________________________________________________________ 

package com.mastermoin.ICore;

public abstract class AbstractDemo  implements InterfaceOne,InterfaceTwo{

       @Override
       public void methodFromInterfaceOne() {
              System.out.println("From Interface One : implements in Abstract Class ");
       }

}

 __________________________________________________________________________

package com.mastermoin.java;

import com.mastermoin.ICore.AbstractDemo;

public class DemoRun extends AbstractDemo{

       public static void main(String[] args) {
              DemoRun obj = new DemoRun();
              obj.methodFromInterfaceOne();
              obj.methodFromInterfaceTwo();
       }

       @Override
       public void methodFromInterfaceTwo() {
              System.out.println("From Interface Two & Abstract Class : Implements in DemoRun Class");
             
       }

}
  __________________________________________________________________________
Output

From Interface One : implements in Abstract Class
From Interface Two & Abstract Class : Implements in DemoRun Class

JAVA Implement Multiple Interface

package com.mastermoin.ICore;

public interface InterfaceOne {
       void methodFromInterfaceOne();
}

________________________________________________________________________

package com.mastermoin.ICore;

public interface InterfaceTwo {
       void methodFromInterfaceTwo();
}
________________________________________________________________________ 

package com.mastermoin.java;

import com.mastermoin.ICore.InterfaceOne;
import com.mastermoin.ICore.InterfaceTwo;

public class DemoRun implements InterfaceOne,InterfaceTwo{

       public static void main(String[] args) {
              DemoRun obj = new DemoRun();
              obj.methodFromInterfaceOne();
              obj.methodFromInterfaceTwo();
       }

       @Override
       public void methodFromInterfaceTwo() {
              System.out.println("From Interface Two");
             
       }

       @Override
       public void methodFromInterfaceOne() {
              System.out.println("From Interface One");
             
       }
}
 __________________________________________________________________________
Output

From Interface One
From Interface Two

Wednesday, 3 September 2014

JAVA How to Avoid Generic type Declaration for ArrayList and HashTable

instate of writing all time generic type declaration for creating ArrayList of any collection type, Java inheritance concept will always useful to create own ArrayList or any type of collection object to reduce task of all time generic form of object declaration 

Normal concept to use generic as : 

ArrayList<UserBean> userList = new ArrayList<UserBean>();

after doing some extra task as :

public class UserBeanArrayList extends ArrayList<UserBean>{

}

cool implementation without generic type declaration, its looking so simple

UserBeanArrayList userArrayList = new UserBeanArrayList();

___________________________________________________________________________________
Why i am doing Extra Task
  • Simple object declaration
  • override some method of ArrayList class as per business logic is possible here
  • add some additional property and method along with list object are also permitted here like normal class
  • Also make this ArraList is singleton class through out your application like other singleton class