java获取当前时间

java获取当前时间,第1张

因为你的这一句 Calendar c = CalendargetInstance();是写在循环外面的,声明了c以后那c就是这一刻的时间,无论你再怎么循环还是只打印声明c时的时间。这段代码我没太看明白你要做什么,不过你想循环输出当前的时间可以这样写:

while(true){

Calendar c = CalendargetInstance();

int time = cget(CalendarSECOND);

Systemoutprintln(time);

}

把Calendar c = CalendargetInstance();写在循环里就是不停的循环获得当前时间。

代码如下:

String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";

Date dNow = new Date();   //当前时间

Date dBefore = new Date();

Calendar calendar = CalendargetInstance(); //得到日历

calendarsetTime(dNow);//把当前时间赋给日历

calendaradd(CalendarDAY_OF_MONTH, -1);  //设置为前一天

dBefore = calendargetTime();   //得到前一天的时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式

String defaultStartDate = sdfformat(dBefore);    //格式化前一天

String defaultEndDate = sdfformat(dNow); //格式化当前时间

Systemoutprintln("前一天的时间是:" + defaultStartDate);

Systemoutprintln("生成的时间是:" + defaultEndDate);

java简介:

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等

用Date curDate = new Date()多点吧、

Calendar rightNow = CalendargetInstance();会创建一个实例,通过

rightNowgetTime()方法来获取当前时间。

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

NTP概念简介 

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

java实现:

import javaioInputStream;

import javanetSocket;

public class TimeUtil {

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

    public static final String DEFAULT_HOST = "time-nwnistgov";//NTP服务器地址

    private TimeUtil() {

    };

    public static long currentTimeMillis(Boolean sync) {

        if (sync != null && syncbooleanValue() != true)

            return SystemcurrentTimeMillis();

        try {

            return syncCurrentTime();

        } catch (Exception e) {

            return SystemcurrentTimeMillis();

        }

    }

    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 = TimeZonegetTimeZone("GMT"); Calendar epoch1900 =

          CalendargetInstance(gmt); epoch1900set(1900, 01, 01, 00, 00, 00);

          long epoch1900ms = epoch1900getTime()getTime(); Calendar epoch1970

          = CalendargetInstance(gmt); epoch1970set(1970, 01, 01, 00, 00, 00);

          long epoch1970ms = epoch1970getTime()getTime();

          

          long differenceInMS = epoch1970ms - epoch1900ms; long

          differenceBetweenEpochs = differenceInMS/1000;

         /

        InputStream raw = null;

        try {

            Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);

            raw = theSocketgetInputStream();

            long secondsSince1900 = 0;

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

                secondsSince1900 = (secondsSince1900 << 8) | rawread();

            }

            if (raw != null)

                rawclose();

            long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;

            long msSince1970 = secondsSince1970  1000;

            return msSince1970;

        } catch (Exception e) {

            throw new Exception(e);

        }

    }

}

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

     server 133100118 prefer 

     server 2107214544 

     server 20311718036 //程序中所用的 

     server 131107110 

     server timeasiaapplecom 

     server 642369653 

     server 1301491721 

     server 669268246 

     server >

java在当前系统时间加一天主要是使用calendar类的add方法,如下代码:import javautilCalendar;import javautilDate;public class ceshi { public static void main(String[] args) { Date date = new Date();// 新建此时的的系统时间 Systemoutprintln(getNextDay(date));// 返回明天的时间 } public static Date getNextDay(Date date) { Calendar calendar = CalendargetInstance(); calendarsetTime(date); calendaradd(CalendarDAY_OF_MONTH, +1);//+1今天的时间加一天 date = calendargetTime(); return date; }}运行结果:

以上就是关于java获取当前时间全部的内容,包括:java获取当前时间、java获取当前时间的前一天、java中获取当前时间一般趋向于用那种方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9812146.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存