Thursday, March 1, 2012


This is a code that can be used to get birth day from National Identity Card in Sri Lanka.  

public Calendar getBirthDate(String nic) {
        int date = Integer.valueOf(nic.substring(2, 5));
        int bdate = 0;
        int bmonth = 0;
        int byear = Integer.valueOf(nic.substring(0, 2));
        if (date > 500) {
            date = date - 500;
        }
        if (date <= 31) {
            bdate = date;
            bmonth = 1;
        }
        if (31 < date & date <= 60) {
            bdate = date - 31;
            bmonth = 2;
        }
        if (60 < date & date <= 91) {
            bdate = date - 60;
            bmonth = 3;
        }
        if (91 < date & date <= 121) {
            bdate = date - 91;
            bmonth = 4;
        }
        if (121 < date & date <= 152) {
            bdate = date - 121;
            bmonth = 5;
        }
        if (152 < date & date <= 182) {
            bdate = date - 152;
            bmonth = 6;
        }
        if (182 < date & date <= 213) {
            bdate = date - 182;
            bmonth = 7;
        }
        if (213 < date & date <= 244) {
            bdate = date - 213;
            bmonth = 8;
        }
        if (244 < date & date <= 274) {
            bdate = date - 244;
            bmonth = 9;
        }
        if (274 < date & date <= 305) {
            bdate = date - 274;
            bmonth = 10;
        }
        if (305 < date & date <= 335) {
            bdate = date - 305;
            bmonth = 11;
        }
        if (335 < date & date <= 366) {
            bdate = date - 335;
            bmonth = 12;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        byear = getYear(byear);
        calendar.set(Calendar.YEAR, byear);
        calendar.set(Calendar.MONTH, bmonth);
        calendar.set(Calendar.DATE, bdate);
        return calendar;
    }

Calculate age in java


Use following code to calculate the age in java. Input parameter is calendar which contains b'day year, month, day.

public int calculateAge(Calendar bcalendar) {
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - bcalendar.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < bcalendar.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        return age;
}

Leaf year in java


Following code can be used to find Leaf Year in java.

public boolean isLeafYear(int year) {
        boolean leaf = false;
        if (year < 100) {
            if (year > 40) {
                year = year + 1900;
            } else {
                year = year + 2000;
            }
        }
        if (year % 4 == 0) {
            if (year % 100 != 0) {
                leaf = true;
            } else if (year % 400 == 0) {
                leaf = true;
            }
        }
        return leaf;
    }

Monday, January 30, 2012

Embed Image in Email Using java

Following code snippet can be used to send email with image in the HTML body.

Note :- HTML body must be contain image tag as follows.

<img height="55" src="cid:image-id" width="65" />

Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp host ip ");

Session session = Session.getDefaultInstance(properties, null);

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());



MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setDataHandler(new DataHandler(new ByteArrayDataSource(body, "text/html")));

Multipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);



MimeBodyPart imagePart = new MimeBodyPart();
DataSource fds = new FileDataSource("image path ");
imagePart.setDataHandler(new DataHandler(fds));
imagePart.setFileName("logo.jpg");
imagePart.setHeader("Content-ID", "<
image-id>");
imagePart.setDisposition(MimeBodyPart.INLINE);
multipart.addBodyPart(imagePart);



message.setContent(multipart);
Transport.send(message);

Tuesday, January 24, 2012

dwr mapping filter

<filter>
<filter-name>dwrFilter</filter-name>
<filter-class>org.directwebremoting.servlet.DwrWebContextFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>dwrFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



This filter can be used to redirect request for DWR servlet.

Monday, November 7, 2011

Truncate nohup.out file in linux

While sending your program output to file called nohup.out, you can't simply delete the file. Since, it is handled in server memory. In that case, you have to empty file using following command.

If you delete nohup.out, while running your program, it will leads to server memory growing and become server unstable.

change the working directory to location where your nohup.out file reside. Then execute the following command to empty the file.

cat /dev/null >nohup.out

NOTE :- nohup.out is in linux for gathering program output data. If you run application as following output will redirect to the nohup.

nohup java -jar testApp.jar

Tuesday, October 25, 2011

Find multicast groups

In windows, use following  commands to find multicast groups.

Open command prompt and run the following command in terminal.

netsh interface ip show joins