Thursday, August 7, 2008

get around Date, blow your date's mind, Customize

This is one sweet utility that I have used over and over again. Honestly I keep forgetting the exact way everytime I need to format date(the java type ;)

Nice to keep it in a little utility program inside the utility package of your project


Example 1:
Date exampleDate;
exampleDate = formatDateTime(useFormat);
private Date formatDateTime(String dateStr){
Date newDate = null;
DateFormat outdfm = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss");//"MM-dd-yy HH:mm:ss"//MMM dd hh:mm:ss yyyy
try{
newDate = outdfm.parse(dateStr);
}
catch (ParseException e){
e.printStackTrace();
}
return newDate;
}

Example 2:
Date curDate = new Date();
Calendar cal = Calendar.getInstance();
String dateStr = "2009-03-19T10:10:51";
curDate = formatDateTime(dateStr);
cal.setTime(curDate);
private static Date formatDateTime(String dateStr){
Date newDate = null;
//DateFormat outdfm = new SimpleDateFormat("MMM-dd-yyyy HH:mm:ss");//"MM-dd-yy HH:mm:ss"//MMM dd hh:mm:ss yyyy
DateFormat outdfm = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss");// 2009-03-19T10:10:51//"yyyy.MM.dd G 'at' hh:mm:ss z"->> 1996.07.10 AD at 15:08:56 PDT
try{
newDate = outdfm.parse(dateStr);
}catch (ParseException e){
e.printStackTrace();
}
return newDate;
}


Thanks for reading, and Thanks for stopping by!!

~Nirmal

Today is the Tomorrow we worried about yesterday
~from somebody's email signature

Tuesday, July 8, 2008

Singleton means: instance, static, CloneNotSupported exceptoin and overriding constructor...

Whenever we(a majority of us, at least most people I have worked with) think of singleton, the idea of "static" comes into picture. But I always like to reiterate that it is not just static.

Developers in any language want a singleton because they are dealing with memory management and security of the code at run time.

Most of the time the "resources" we load when the application starts are going to be the same. For those things in application which strictly should be created once.

Singleton design for such objects can be adapted, and by that it just doesn't mean that we can do:

private NameOfSingletonClass(){
}

It must be more than that, most specifically: the code must account for the fact that any other class which gets a copy of the instance of the singleton class can always create a copy by using .clone(). Any java class can override the clone() method of type Object because all classes in java are descendants of the "grand daddy" class in java called "Object".

So to prevent that: we must also put a block of code as follows:

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

Now, to summarize on creating singleton class, please refer to the complete singleton class below, this is the best and the perfect in its grade to implement a singleton pattern(of course you will need it in your local package and named appropriately with other of your custom methods for your custom logic.

/**
* Object Comments go here...TODO
*/
package com.nirmal.singh.designpatterns.singleton
/**
* @author nsingh
*
*/

public class RealSingleton {

private static RealSingleton singleTonObjVar;

private RealSingleton() {
//no code implementation needed here
//this is just to override the default constructor
}

public static RealSingleton getSingletonObject() {
//checking for null to find out if it is the first time
//this singleton needs to be created
if (singleTonObjVar == null)
singleTonObjVar = new RealSingleton();
return singleTonObjVar;
}

//any java class can invoke a clone() method to clone this
//singleton if we do not include the method below
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}

Thanks for reading...happy coding!!