Setting Classpath

19 04 2009

Set ClassPath
export CLASSPATH=$CLASSPATH:/java/build/classes/

Set Path
export PATH=$PATH:/mnt/home/jdk1.6/bin/

Advertisements




Sort Multiple Fields With Collections.sort

25 03 2008

ComparatorChain chain = new ComparatorChain();
Comparator comp = new BeanComparator("firstname", new NullComparator(true));
chain.addComparator(comp,true);
comp = new BeanComparator("lastname", new NullComparator(true));
chain.addComparator(comp);
Collections.sort(customerlist,chain);

NullComparator(true) <– true places null values at the front of the list
chain.addComparator(comp,true); <– true reverse the list
customerlist = list of dynabeans





Custom Format Number

25 03 2008

DecimalFormatSymbols sym = new DecimalFormatSymbols();
sym.setGroupingSeparator('-');
NumberFormat accFormatter = new DecimalFormat("###,###,####,######",sym);
BigDecimal num = new BigDecimal("1111111111111111");
String formattedNum = accFormatter.format(num);

formattedNum = 111-111-1111-111111





Create List Of Dates With DynaBean

27 09 2007

Create list of dates (last 6 months) to populate listbox in .jsp.

// get search date listbox
DynaBean dateBean = new LazyDynaBean();
List<DynaBean> dateList = new ArrayList<DynaBean>();
SimpleDateFormat formatter = new SimpleDateFormat("MMM yyyy");
// display 6 months starting from current month
Calendar cal = Calendar.getInstance();
// get current date
cal.setTime(new Date());
for(int i=0; i<5; i++) {
dateBean = new LazyDynaBean();
dateBean.set("property", formatter.format(cal.getTime()));
dateBean.set("value", cal.getTime());
// add to list
dateList.add (dateBean);
// subtract 1 month
cal.add(Calendar.MONTH, -1);
}
// send to form bean (type="java.util.List")
PropertyUtils.setUtils(form, "datelist", dateList);

in .jsp


<bean:define id="dates" name="MyFormName" property="datelist"/>
<html:select property="s_valueDate" value='<%= request.getParameter("s_valueDate") %>'> <html:options collection="dates" property="value" labelProperty="property"/>
</html:select>





Get Last Date Of The Month

27 09 2007

String valueDate= "09/2007";
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("MM/yyyy").parse(valueDate));
String endDate = cal.getActualMaximum(Calendar.DAY_OF_MONTH) + "/"+ valueDate;

endDate = 30/09/2007;





Set Not To Store Page In Cache

11 09 2007

<head>
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
</head>





Remove TimeStamp From Date Datatype

25 08 2007

warghhhh..why are date datatypes such a headache?! 😦

Tis was the easiest way i could think off without using some gungho algorithm 😀 Date -> String -> Date

// format current date (without time) and convert to string
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String todayDate = formatter.format(new Date());
Date formattedDate = null;
try {
// convert date string back to Date datatype
formattedDate = formatter.parse(todayDate);
} catch (ParseException e) {
log.error("Parse today date error");
}