Wednesday, 22 January 2014

Java Useful Codes and Useful links

Java EE  Vs Spring 

http://www.slideshare.net/reza_rahman/java-ee-and-spring-sidebyside

Java Basic
http://docs.oracle.com/javase/tutorial/extra/certification/index.html
java  report in spring
http://lagivan.blogspot.in/2011/11/dynamicreports-and-spring-mvc.html

Excel Creation in java

http://poi.apache.org/spreadsheet/examples.html

http://poi.apache.org/spreadsheet/quick-guide.html

Java Null Pointer Check  Code
/**
The Following method is used  for check whether string is null or not
return true -- if string is null
false  -- if string not null
*/

public boolean isNullValue( String  stringVal){

     if(stringVal ==null || "".equals(stringVal) || "null".equalsIgnoreCase(stringVal)){

       return true;

      }else{

       return false;

     }

}


Drag and drop files jquery.
http://hayageek.com/drag-and-drop-file-upload-jquery/


Run bat file from java.
Process proc = Runtime.getRuntime().exec(new String[] {"bat file path",arguments to bat file});
BufferedReader b = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = "";

            while ((line = b.readLine()) != null) {
            System.out.println(line);
            }

Example 
Process proc = Runtime.getRuntime().exec(new String[] {"d:\\batchFile.bat",arguments to bat file});


By using bat file send arguments to java program.

In bat file:
java fileName %1 %2
First argument %1
second argument %2;

Append String in comma for as separator
int count=0
StringBuffer fileIds= new StringBuffer();

while(rsFileDetails.next()){
  if (count != 0) {
   fileIds.append(",");
  }
  fileIds.append(rsFileDetails.getString(1));
  count++;
}


Java schedule task

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 
 * @author B.Balamurugan This file is used for the schedule the task(Job) using
 *         in java Once this class get run as this example it will continues run
 *         every 5 min
 * 
 */
public class JavaScheduleTask extends TimerTask {

 @Override
 public void run() {
  // Here we have to mention the task
  System.out.println(new Date());

 }

 public static void main(String args[]) {
  int initialDelay = 5000; // start after 5 seconds
  int period = 5 * 60 * 1000; // repeat every 5 minutes
  TimerTask timerTask = new JavaScheduleTask();
  // running timer task as daemon thread
  Timer timer = new Timer();

  timer.scheduleAtFixedRate(timerTask, initialDelay, period);

 }

}
Java Folder contains File
/**
     * This method is used for the specified folder contains the file or not
     * 
     * @param filePath
     * @param fileName
     * @return true folder contains the file false folder not contains the file
     */
    public boolean isFolderContainsFile(String folderPath, String fileName) {
        File file = new File(filePath);
        List<String> fileNames = new ArrayList<String>();
        if (file.isDirectory()) {
 
            for (File fileName1 : file.listFiles()) {
                fileNames.add(fileName1.getName());
 
            }
 
        }
        return fileNames.contains(fileName);
    }


Java to generate alphanumeric  random  number 6 digit

Random rand = new Random();
  String ranNo="";
    for (int j = 0; j<3; j++){
       int pick = (int) (Math.random() * 10);
       double randomNumberSetup = (pick * 2 + 'a');
       char randomCharacter = (char) randomNumberSetup;
      ranNo=ranNo+pick+randomCharacter;       
     }
 System.out.println("randam Number"+ranNo); 


File Name Renameing

String fileName = fileName.substring(0, fileName.lastIndexOf("epub"))+"doc";
Java To Find current week start date and end date

        Calendar currentDate    = Calendar.getInstance();
 Calendar startOFweek    = Calendar.getInstance();
 Calendar endDateOFWeek  = Calendar.getInstance();
 
        int dayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);
 int endDateDiff = 7-dayOfWeek;
 
 startOFweek.add(Calendar.DATE, -(dayOfWeek-1));
 endDateOFWeek.add(Calendar.DATE, endDateDiff);
 
 System.out.println("Start date Of week "+startOFweek.getTime());
 System.out.println("End date of week "+endDateOFWeek.getTime());




No comments:

Post a Comment