关于java时间gmt的信息

博主:adminadmin 2023-01-15 05:18:10 454

本篇文章给大家谈谈java时间gmt,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

我用JAVA 直接打印一个DATE 怎么显示的是GMT时间

因为默认locale不是us,所以就打印gmt时间了,看源代码

timezone

zi

=

date.getzone();

if

(zi

!=

null)

{

sb.append(zi.getdisplayname(date.isdaylighttime(),

zi.short,

locale.us));

//

zzz

}

else

{

sb.append("gmt");

}

sb.append('

').append(date.getyear());

//

yyyy

return

sb.tostring();

}

java中怎么将日期转换为GMT格式

其实不管建不建议,能用就行,不非得用推荐的。

String toGMT(Date date) {

try {

SimpleDateFormat format = new SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss");

Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));

format.setCalendar(cal);

return format.format(date);

} catch (ParseException e) {

e.printStackTrace();

}

}

java获取网络当前时间

如果你要获取的是Internet时间,可以使用NTP服务。

NTP概念简介 

Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。

java实现:

import java.io.InputStream;

import java.net.Socket;

public class TimeUtil {

    public static final int DEFAULT_PORT = 37;//NTP服务器端口

    public static final String DEFAULT_HOST = "time-nw.nist.gov";//NTP服务器地址

    private TimeUtil() {

    };

    public static long currentTimeMillis(Boolean sync) {

        if (sync != null  sync.booleanValue() != true)

            return System.currentTimeMillis();

        try {

            return syncCurrentTime();

        } catch (Exception e) {

            return System.currentTimeMillis();

        }

    }

    public static long syncCurrentTime()  throws Exception {

        // The time protocol sets the epoch at 1900,

        // the java Date class at 1970. This number

        // converts between them.

        long differenceBetweenEpochs = 2208988800L;

        // If you'd rather not use the magic number uncomment

        // the following section which calculates it directly.

        /*

         * TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =

         * Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);

         * long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970

         * = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);

         * long epoch1970ms = epoch1970.getTime().getTime();

         * 

         * long differenceInMS = epoch1970ms - epoch1900ms; long

         * differenceBetweenEpochs = differenceInMS/1000;

         */

        InputStream raw = null;

        try {

            Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);

            raw = theSocket.getInputStream();

            long secondsSince1900 = 0;

            for (int i = 0; i  4; i++) {

                secondsSince1900 = (secondsSince1900  8) | raw.read();

            }

            if (raw != null)

                raw.close();

            long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;

            long msSince1970 = secondsSince1970 * 1000;

            return msSince1970;

        } catch (Exception e) {

            throw new Exception(e);

        }

    }

}

中国大概能用的NTP时间服务器 

     server 133.100.11.8 prefer 

     server 210.72.145.44 

     server 203.117.180.36 //程序中所用的 

     server 131.107.1.10 

     server time.asia.apple.com 

     server 64.236.96.53 

     server 130.149.17.21 

     server 66.92.68.246 

     server  

     server 18.145.0.30 

     server clock.via.net 

     server 137.92.140.80 

     server 133.100.9.2 

     server 128.118.46.3 

     server ntp.nasa.gov 

     server 129.7.1.66 

     server ntp-sop.inria.frserver 210.72.145.44(国家授时中心服务器IP地址) 

     ntpdate 131.107.1.10 

     ntpdate -s time.asia.apple.com

在java中,怎么得到用户输入的日期时间与GMT相距的时间差

一般情况下不会出现如题所示的问题,既然出现了我们就要想办法解决之。以我的机器为例,我的当前系统时间是北京时间2011-08-31T08:00:00,但是newDate()得到的却是:2011-08-31T00:00:00。表示java得到的时间比当前系统时间提前了8个小时,那么我们就要想办法把java得到的时间向后推迟8个小时。解决的办法是:

1、找到你用的jre文件夹,以本机为例:C:\Program Files\Java\jdk1.5.0\jre

2、在上述文件夹下有个zi文件夹,我们的工作都在这里。

3、zi文件夹下有一个Etc文件夹,我们要做的工作就是从这个文件夹下找到对应的GMT文件替换zi下GMT文件。

4、选择哪个文件呢?如果java获取的时间+N=当前系统时间,就选择GMT-N文件,重命名为GMT,覆盖掉zi下的GMT文件,如果java获取的时间-N=当前系统时间,就选择GMT+N文件,重命名为GMT,覆盖掉zi下的GMT文件。

5、再运行一下你的程序看时间是不是正确了,如果还是不正确,请确认你的jre目录是否选得正确。

java时间格式转换 Thu Dec 24 17:33:00 GMT+08:00 2015 我想转

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

Date date = new Date();

String str=df.format(date);

java怎么获取gmt当前的系统时间

Date now = new Date(); 当前时间就是now,你试着打印一下就出来了,如果对日期时间格式有要求,就SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式,不就OK了

java时间gmt的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java时间gmt的信息别忘了在本站进行查找喔。