Java获取Ntp服务器时间

Java获取Ntp服务器时间

package demo.test.ntp;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import org.apache.commons.net.ntp.TimeStamp;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class NtpTest {

    public static void main_bak(String[] args) {
        StringBuilder sb = new StringBuilder();
        //sb.append("\n").append(" 11 MD5 Ib[:*M=OW4eDTxFk*`{I  # MD5 key");//追加时使用
        sb.append(" 11 MD5 Ib[:*M=OW4eDTxFk*`{I  # MD5 key");
        File conf = new File("C:\\Users\\EST\\Desktop\\ntp\\keys2");
        try {
            //FileUtils.writeStringToFile(conf, sb.toString(),true);//追加时使用
            FileUtils.writeStringToFile(conf, sb.toString(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            Date formDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse("Thu Feb 10 17:00:51 CST 2022");
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(formDate);
            System.out.println(format);

            Date formDate2 = new SimpleDateFormat("dd MMM HH:mm:ss", Locale.US).parse("10 Feb 16:46:06");
            String format2 = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA).format(formDate2);
            System.out.println(format2);

            Date formDate3 = new SimpleDateFormat("EEE, MMM dd yyyy HH:mm:ss.SSS", Locale.US).parse("Mon, Feb 14 2022 14:02:09.439");
            String format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(formDate3);
            System.out.println(format3);

            Date formDate4 = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS", Locale.US).parse("Feb 14 2022 14:02:09.439");
            String format4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(formDate4);
            System.out.println(format4);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(getDateTime("cn.pool.ntp.org"));
    }

    public static String getDateTime(String host) {
        NTPUDPClient timeClient = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            timeClient = new NTPUDPClient();
            timeClient.setDefaultTimeout(10000);
            InetAddress timeServerAddress = InetAddress.getByName(host);
            TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
            TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
            Date date = timeStamp.getDate();
            //系统获取:Thu Feb 10 17:00:51 CST 2022
            //服务器date命令查询:Thu Feb 10 17:00:51 CST 2022
            //服务器ntpdate命令查询:10 Feb 16:46:06
            return dateFormat.format(timeStamp.getDate());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (timeClient != null) {
                timeClient.close();
            }
        }
        return null;
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值