Friday, May 13, 2016

Daylight Savings in Java : Find the Offset in GMT



The Code Snippet provides the Offset in GMT , When there is a 

Daylight savings for a respective time zone then the below code 

snippet provides the hours and minutes of how much the time 

should be offset in GMT Standard.

Code Snippet :

       public static String getCurrentTimezoneOffset(TimeZone timeZoneInstance) {
         Calendar _calenderInstance = GregorianCalendar.getInstance(timeZoneInstance);
         int offsetInMillis = timeZoneInstance.getOffset(_calenderInstance.getTimeInMillis());
         String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
         offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
         return "GMT"+offset;

       }

Transform Map to CSV (Comma Separated Values) Acceptable Acceptable String



Code Snippet :

       /**
        * Transform mapto string instances.
        *
        * @param propertymapperValues the propertymapper values
        * @return the string
        */
       private static String transformMaptoStringInstances(Map<String,String> propertymapperValues){
         StringBuilder _buildCSVContends = new StringBuilder();
         for(Entry<String,String> entryInstances : propertymapperValues.entrySet()){
           _buildCSVContends.append("\""+entryInstances.getKey()+"\"");
           _buildCSVContends.append(",");
           _buildCSVContends.append("\""+entryInstances.getValue()+"\"");
           _buildCSVContends.append("\n");
         }
         return _buildCSVContends.toString();
       }

Convert All data from Properties to a Hash Map In Java


Code Snippet :

/** The propertied values. */
       static Map<String,String> propertiedValues = new HashMap<String, String>();
       static{
         try {
              propertiedValues = getPropertiesAsMap(MESSAGE_PROPERTIES_PATH);
              System.out.println("The Size of the propertiedValues map is "+propertiedValues.size());
         } catch (FileNotFoundException fileNotFoundException) {
              fileNotFoundException.printStackTrace();
         } catch (IOException ioException) {
              ioException.printStackTrace();
         }
       }
      
       /**
        * Gets the properties as map.
        *
        * @param propertyFilePath the property file path
        * @return the properties as map
        * @throws FileNotFoundException the file not found exception
        * @throws IOException Signals that an I/O exception has occurred.
        */
       public static Map<String,String> getPropertiesAsMap(String propertyFilePath) throws FileNotFoundException, IOException{
         Properties properties = new Properties();
         Map<String,String> mapOfValues = new HashMap<String, String>();
         properties.load(new FileInputStream(propertyFilePath));
         for (String key : properties.stringPropertyNames()) {
           String value = properties.getProperty(key);
           mapOfValues.put(String.valueOf(key.trim()),value.trim());
         }
         return mapOfValues;
       }

Thursday, May 12, 2016

Get All Files from folder Including from Sub Folders In Java


Code Snippet :

       /**
        * Gets the all files from folder.
        *
        * @param directoryName the directory name
        * @param files the files
        * @return the all files from folder
        */
       public static void getAllFilesFromFolder(String directoryName, ArrayList<File> files) {
         File directory = new File(directoryName);
         File[] fList = directory.listFiles();
         for (File fileInstance : fList) {
           if (fileInstance.isFile() && fileInstance.getName().endsWith(".xhtml")) {
             files.add(fileInstance);
           } else if (fileInstance.isDirectory()) {
             getAllFilesFromFolder(fileInstance.getAbsolutePath(), files);
           }
         }
       }


The above code snippet gets all the files from the folders including the files in the sub folders and then check whether the file name ends with .xhtml and filters the files which are of Extension .xhtml.
Similarly users can filter the required file formats using the required file extensions

Write String in to CSV (Comma Separated Values) File in Java







Code Snippet :

       public static void writeStringAsCSV(String resultsetContentds,String filePath){
      FileOutputStream fileoutputStream = null;
         File fileInstance;
         try {
           fileInstance = new File(filePath);
              fileoutputStream = new FileOutputStream(fileInstance);
              if (!fileInstance.exists()) {
                fileInstance.createNewFile();
              }
              byte[] contentInBytes = resultsetContentds.getBytes();
              fileoutputStream.write(contentInBytes);
              fileoutputStream.flush();
              fileoutputStream.close();
         } catch (IOException exceptionTrace) {
           exceptionTrace.printStackTrace();
         } finally {
           try {
                if (fileoutputStream != null) {
                  fileoutputStream.close();
                }
              } catch (IOException exceptionTrace) {
                exceptionTrace.printStackTrace();
              }
         }

       }

Wednesday, September 30, 2015

Easily remove an Element In Java 8.0 Using removeIf






The collection also makes it super simple to remove elements from a collection. We just use a lambda expression, a predicate, to express which elements we want removed




Wednesday, September 25, 2013

The Only Way to DO GREAT WORK is to love what you do - STEVE JOBS




For the past 33 years I have looked in the mirror every morning and asked myself , if today were the last day in my life what I want to do what I am about to do and whenever the answer is no for too many days in a row I know I need to change something remembering that all  be dead soon is the important tool I have ever encountered help me make the big choices in life , because almost everything all external expectations all pride all future embarrassment of failure is far away in the face of death , living only is truly important , Remembering that you are going to die is the best way I know to avoid the trap of thinking something to lose, You are already Naked there is no reason not to follow your heart ,About a year ago I was diagnosed with cancer I had a scan at 7.30 in the morning it clearly showed a tumor in my pancreas, I dint even know what pancreas was , The doctors told me  this was certainly a type of cancer that is incurable and I should expect a little of no longer than 3 to 6 months, My doctor advised me to go home and get my affairs in order which is doctors code for prepared to die , it means to try and tell your kids everything you thought that you have ten years to tell in few months , it means to make sure everything is buttoned up so that it would be easiest possible for your family, It means you to say Good Bye !!!!

- Steve jobs