Java JSP – JSTL Date Format Example

This example show how to format date in JSP using format tag library.

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
</head>

<body>
    <jsp:useBean id="date" class="java.util.Date"/>
    Today is: <fmt:formatDate value="${date}" type="date" pattern="dd-MMM-yyyy"/>
</body>

</html>

Java – Generic Constructor Example

class GenericConstructor {
    private double value;

    <T extends Number> GenericConstructor(T arg) {
        value = arg.doubleValue();
    }

    void showValue() {
        System.out.println("value: " + value);
    }
}

public class GenericConstructorTest {

    public static void main(String args[]) {
        GenericConstructor gc1 = new GenericConstructor(100);
        GenericConstructor gc2 = new GenericConstructor(123.5F);

        gc1.showValue();
        gc2.showValue();
  }

}

Output:

value: 100.0
value: 123.5

Java – Get MAC Address

Since JDK 1.6, Java developers are able to access network card detail via NetworkInterface class. In this example, we show you how to get the localhost MAC address in Java.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MacAddress {

    public static void main(String[] args) {

        InetAddress ip;

        try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();

            System.out.print("Current MAC address : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            System.out.println(sb.toString());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e){
            e.printStackTrace();
        }

    }

}

Output:

Current IP address : 192.168.1.22
Current MAC address : 00-26-B9-9B-61-BF

Note: The NetworkInterfaceNetworkInterface.getHardwareAddress() method is only allowed to access localhost MAC address, not remote host MAC address.

Page 3 of 4912345...102030...Last »