java 获取http地址_java如何获取当前时间,java如何获取ip地址

展开全部

获取当前时间public static void main(String[] args) throws IOException {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println(format.format(new Date()));

}

获取ip地址public static void main(String[] args) throws IOException {

InetAddress netAddress = InetAddress.getLocalHost();

System.out.println("ip地址: " + netAddress.getHostAddress());

System.out.println("主机名: " + netAddress.getHostName());

}

其实使用InetAddress.getLocalHost()获取ip存在问题32313133353236313431303231363533e78988e69d8331333365653765,因为有的电脑有多个网卡,也就有多个ip地址,正确的方法应该是这样public static void main(String[] args) throws IOException {

List result = new ArrayList();

Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();

InetAddress ip;

while (netInterfaces.hasMoreElements()) {

NetworkInterface ni = netInterfaces.nextElement();

Enumeration addresses = ni.getInetAddresses();

while (addresses.hasMoreElements()) {

ip = addresses.nextElement();

if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(':') == -1) {

result.add(ip.getHostAddress());

}

}

}

for (String string : result) {

System.out.println(string);

}

}

使用jdk提供的api NetworkInterface,可以将主机的所有网卡的ip地址全部获取,比如无线网卡、有线网卡、蓝牙等等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值