Check to see how many days difference between the current date and the date passed in as a parameter.

Days from today

public static int daysFromToday(Calendar c) {
    Calendar today = Calendar.getInstance();
    int dayDifference = c.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
    int yearDifference = c.get(Calendar.YEAR) - today.get(Calendar.YEAR);
    return dayDifference + (365 * yearDifference);
}

Add a comment

Check to see if the device has network connectivity

public static boolean isConnectedToInternet(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
      NetworkInfo[] info = connectivity.getAllNetworkInfo();
      if (info != null)
        for (int i = 0; i < info.length; i++)
          if (info[i].getState() == NetworkInfo.State.CONNECTED) {
            return true;
          }

    }
    return false;
}

Add a comment

This wrapper class adds the ability to turn off logging using a simple boolean flag. It also let's you set a tag in one place, meaning you don't have to type it over and over again throughout your code. The only parameter needed is the String to get logged.

Usage:

Logger.i("this is your log message");

Notice, no repetitive tag parameter!

Custom Logger Class:

package com.pepinonline.common;

import android.util.Log;

public class Logger {

  private static final boolean DEBUG = true;
  private static String TAG = "PEPIN";
  
  public static void d(String string) {
    if(DEBUG) {
      Log.d(TAG, string);
    }
  }
  
  public static void v(String string) {
    if(DEBUG) {
      Log.v(TAG, string);
    }
  }
  
  public static void i(String string) {
    if(DEBUG) {
      Log.i(TAG, string);
    }
  }
  
  public static void e(String string) {
    if(DEBUG) {
      Log.e(TAG, string);
    }
  }
  
  public static void w(String string) {
    if(DEBUG) {
      Log.w(TAG, string);
    }
  }

}

 

Add a comment

More Articles ...

Page 1 of 2