23/08/2013

[Android] Auto Answer app

Last year I bought an Android smartphone from LG, with Gingerbread 2.3.4 on it. Much to my surprise, it was not very smart, for example I couldn't set it to automatically answer incoming calls.

The good thing about having it run Android, was that I could fix this flaw by developing my own app, so I googled around and found an Auto Answer project on Google Code from Matt Hahnfeld, released under a GNU GPL v3 licence.

Since the author removed it from Google's Play Store, I catered his code to my needs and installed the app on my Optimus HUB. It works perfectly, I tested it on Ice Cream Sandwich 4.0.3 (LG Optimus L7) without issues, and it should work on Froyo 2.2 too.

At the time there weren't many free apps that did this but now you finally have some choices, note that some phones already come with this feature built-in.

You can find my code on GitHub, the licence remains the same GNU GPL v3. To successfully compile it you'll need an IDE such as Eclipse, and the proper Android SDK for your OS version. The source code I uploaded comes with the Android 4 lib, but you can easily switch it to another version.

22/08/2013

[Java] String to Date and vice versa

When working with dates in Java you'll often find yourself having to convert them from a String object to a Date object and vice versa. These function snippets will allow you to easily perform the conversion operations, no third-party libraries needed:

Imports

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


string2Date

 /** Converts given String in a Date object with specific format  
  * @param date - a String representing the date to convert. IT MUST BE in the given format  
  * @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy  
  * @return a Date object, {@null} if the given string is {@null} or if there's been a conversion error  
  * */  
 public static Date string2Date(String date, String dateFormat){  
      Date d = null;  
      try{  
           if(date!=null && !date.equalsIgnoreCase("") && dateFormat!=null && !dateFormat.equalsIgnoreCase("")) d = new SimpleDateFormat(dateFormat, Locale.ITALIAN).parse(date);//or whatever Locale you need  
      }catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL  
      return d;  
 }  

date2String

 /** Converts given Date in a String object with specific format  
 * @param date - a Date to be represented as a string. IT MUST BE in the given format  
 * @param dateFormat - a String specifying the format of the date e.g: dd/MM/yyyy  
 * @return a String object, {@null} if the given date is {@null} or if there's been a conversion error  
 * */  
 public static String date2String(Date date, String dateFormat){  
      Format formatter = new SimpleDateFormat(dateFormat);  
      String s = null;  
      try{  
           if(date!=null && dateFormat!=null && !dateFormat.equalsIgnoreCase(""))s = formatter.format(date);  
      }catch(Exception e){}//you could always decide to raise the exception rather than returning simply NULL  
      return s;  
 }  

Of course, if you prefer, rather than returning NULL if something went wrong, you could always raise an Exception. You can find all accepted formats in the SimpleDateFormat JavaDoc.