Saturday, July 18, 2009

How to convert string date into Date object.

How to convert string date into Date object.

Date date = getDate("18/07/2009 15:15:45");

private static Date getDate(String fullDate) throws ParseException {
          SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
          df.setLenient(false); return df.parse(fullDate);
}


Wednesday, July 15, 2009

Getting the IP Address of a Hostname

Getting the IP Address of a Hostname

        try {
                InetAddress addr = InetAddress.getByName("google.com");
                byte[] ipAddr = addr.getAddress();

                // Convert to dot representation
                String ipAddrStr = "";
                for (int i=0; i<ipAddr.length; i++) {
                    if (i > 0) {
                        ipAddrStr += ".";
                    }
                    ipAddrStr += ipAddr[i]&0xFF;
                }
            System.out.println("ipAddrStr = " + ipAddrStr);
            } catch (UnknownHostException e) {
            }