Saturday, April 12, 2008

Formatting date from java util date

public String getdd_mm_yyyy(java.util.Date dat)
{
DateFormat df = DateFormat.getDateInstance();
String s = "";
StringBuffer tos = new StringBuffer(s);
FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD);
StringBuffer sb = df.format(dat,tos,fp);
String s1 = sb.toString();
StringTokenizer st = new StringTokenizer(s1," ");
String mon=null , dd=null , yyyy=null;
if (st.hasMoreTokens())
{
mon = st.nextToken();
dd = st.nextToken();
yyyy = st.nextToken();
}
dd = dd.substring(0,dd.length()-1);
if (Integer.parseInt(dd)<10) dd="0"+dd;
s = dd + "-" + mon + "-" + yyyy;
return(s);
}

public java.util.Date getUtilDateFrom_ddmmyyyy(String ddmmyyyy)
{
String tempYear=null,tempMonth=null,tempDay=null;
String strtemp = ddmmyyyy;
StringTokenizer tokenss = new StringTokenizer(strtemp,"/");
if(tokenss.hasMoreTokens())
{
tempDay = tokenss.nextToken();
tempMonth = tokenss.nextToken();
tempYear = tokenss.nextToken();
}
return(new java.util.Date(Integer.parseInt(tempYear)-1900,Integer.parseInt(tempMonth)-1,Integer.parseInt(tempDay)));
}

No comments: