Monday, 27 January 2014

What you should do, and what don't (future example here)

Save time by using
String.format (Object object, StringBuffer buffer, FieldPosition field)(

You shouldn't do:
      public String generateDateString(long timeMilises) {  
           String dateString = "";  
           Calendar c = Calendar.getInstance();  
           c.setTimeInMillis(timeMilises);  

           dateString = ((c.get(Calendar.DAY_OF_MONTH) < 10) ? ("0" + c.get(Calendar.DAY_OF_MONTH))  
                     : (c.get(Calendar.DAY_OF_MONTH)))  
                     + "."  
                     + (((c.get(Calendar.MONTH) + 1) < 10) ? ("0" + (c.get(Calendar.MONTH) + 1))  
                               : (c.get(Calendar.MONTH) + 1)) + "." + c.get(Calendar.YEAR);  
           return dateString;  
      }  
You should do:
      public String generateDateString(long timeMilises) {  
           String dateString = "";  
           Calendar c = Calendar.getInstance();  
           c.setTimeInMillis(timeMilises);  
           
           dateString = String.format("%02d.%02d.%d", c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH)+1, c.get(Calendar.YEAR));

           return dateString;  
      }