Blog Archives

Parsing CronJob Expression in Android

I had been trying to parse a cron expression string in my Android app and came across this CronExpression Code source code. I move it into my project and you can just use it off the shelve by calling

CronExpression conExpression = new CronExpression(“* * 10 * * ?”); // This will construct a cronjob which runs at 10am daily.

After obtaining the object, you can explore to more method that it provides for your business logic.

Advertisements

Simple method to generate random password

   public static String generateTempPassword(int length){
	   String character = "qwertyuioplkjhgfdsazxcvbnm1234567890POIUYTREWQASDFGHJKLMNBVCXZ!@#$%&";
	   StringBuffer sb = new StringBuffer();
	   while(length>0){
		   double index = Math.random() * character.length();
		   sb.append(character.charAt((int)index));
		   length--;
	   }
	   return sb.toString();
   }

Getting Date information from MongoDB’s document objectid

This post is for mongodb java api only.


	private Date getTimeStampFromID() {
        DB db = connectDB();
        DBCollection collection = db.getCollectionFromString("database");
		DBCursor cursor = collection.find();
		while(cursor.hasNext()){
			return (new Date(((ObjectId)cursor.next().get(TAG_ID)).getTime()));
		}
	}

Parsing JSON data using Google GSON library in Eclipse

Setup:
1. Download gson library
2. Add as external jar from your eclipse project

JSON String:
{
“status”:”200″,
“error_message”:”Success”,
“item”:
[{“ID”:”18″, “Name”:”Name A”} ,
{“ID”:”19″, “Name”:”Name B”} ,
{“ID”:”20″, “Name”:”Name C”} ,
{“ID”:”21″, “Name”:”Name D”}]
}

Java class to implement the parsing

public class gsonexample {

	public gsonexample(){}

	public void parseJson(String url){
		try{
			Reader reader = new InputStreamReader(new URL("url").openStream()); //Read the json output
			Gson gson = new GsonBuilder().create();
			DataObject obj = gson.fromJson(reader, DataObject.class);
			System.out.println(obj);
		}catch(Exception e){
			System.out.println(e);
		}
	}

	private class DataObject{ //This class should match your json object structure
		private int status;
		private String error_message;
		private List<Item> item; // This is for the inner array
		@Override
	    public String toString() {
	        return status + " - " + error_message+ " (" + item + ")";
	    }
	}

	private class Item{ //This is the inner array class
		public int ID;
		public String Name;
		@Override
	    public String toString() {
	        return ID + " - " + Name +"\n";
	    }
	}
}

Convert integer array to integer

This is a simple function on how one can convert an integer array (int[]) to integer value

</pre>
public int add(int[] array) {
int len = array.length;
int tmp = 0;
for(int i: array){
tmp += i*((int)Math.pow(10,--len));
}

return tmp;
}
<pre>

Exception java.util.ConcurrentModificationException on ArrayList

If you encounter exception:

Exception in thread “main” java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)

You can go around and use CopyOnWriteArrayList<E>() instead.