简体   繁体   English

如何在不使用任何预定义类或库的情况下用Java获取系统的当前日期和时间?

[英]how can i fetch current date and time of my system in java without using any predefined classes or libraries?

how can i fetch current date and time of my system in java without using any predefined classes or libraries? 如何在不使用任何预定义类或库的情况下用Java获取系统的当前日期和时间? i am actually making a Date ADT of my own from scratch as java.util.Date or Java.util.Calendar. 我实际上是从头开始以java.util.Date或Java.util.Calendar制作自己的Date ADT。 but i am not able to find out a way to fetch system date and time, say to calculate age of somebody if he enters his current age. 但是我无法找到一种获取系统日期和时间的方法,比如说计算某人的年龄(如果他输入了当前年龄)。 in a way i want to learn how they developers of Date or Calendar class actually implemented it. 我想以某种方式来学习他们的Date或Calendar类的开发人员如何实际实现它。 is there a way that i can see the code of Date and Calendar classes defined in java.util package. 有没有一种方法可以查看java.util包中定义的Date和Calendar类的代码。

Well System.currentTimeMillis() isn't in java.util - is that what you were looking for? 那么System.currentTimeMillis()不在java.util -是您要找的东西吗? You'd then have to write the code to work out what that value (milliseconds since the unix epoch) means in terms of years etc - and you should consider the time zone and calendar system. 然后,您必须编写代码以计算出该值(从unix纪元以来的毫秒数)在年份等方面的含义-并且应考虑时区和日历系统。

I would personally avoid trying to write your own date/time package unless it's just for fun. 我个人会避免尝试编写自己的日期/时间包,除非只是为了好玩。 java.util.Date / Calendar are awful, but Joda Time is much better, and there's the upcoming JSR 310 . java.util.Date / Calendar是可怕的,但约达时间要好得多,而且也即将JSR 310

Is there a way that i can see the code of Date and Calendar classes defined in java.util package. 有没有一种方法可以查看java.util包中定义的Date和Calendar类的代码。

Sure - when you installed the JDK, you should have had the option of also installing a copy of the source code for the standard libraries, typically in src.zip . 当然-在安装JDK时,您应该可以选择还为标准库安装源代码的副本,通常是在src.zip Alternatively, you could go to openjdk.java.net and look there. 或者,您可以转到openjdk.java.net并查看。

actually i don't want to use any predefined function at all. 实际上我根本不想使用任何预定义的功能。

Then you'd have to use JNI to get to an underlying system call, which seems pretty pointless to me - as it's basically what System.currentTimeMillis will do. 然后,您必须使用JNI进行基础系统调用,这对我而言似乎毫无意义-因为这基本上就是System.currentTimeMillis要做的事情。 That's fine if you're trying to learn JNI, but it's not really got much to do with date/time. 如果您想学习JNI,那很好,但是与日期/时间关系不大。

Of course if you're interested in an accurate time instead of the *system time, you could implement an NTP client. 当然,如果您对准确的时间而不是*系统时间感兴趣,则可以实现NTP客户端。

Using System.currentTimeMillis() : 使用System.currentTimeMillis()

Returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. 返回当前时间与UTC 1970年1月1日午夜之间的差值(以毫秒为单位)。

As for the source code of java.util.Date and java.util.Calendar , you can either download the source directly from http://jdk7.java.net/source.html , or view it online in docjar.com or here . 至于java.util.Datejava.util.Calendar的源代码,您可以直接从http://jdk7.java.net/source.html下载源代码,也可以在docjar.com此处在线查看源代码。 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不使用任何预定义集合库的购物篮 - Shopping basket without using any predefined collection libraries 如何在 Java 中获取 UTC 或 GMT 的当前日期和时间? - How can I get the current date and time in UTC or GMT in Java? 如何使用Java代码从具有当前日期和时间的文件夹中获取文件名 - How to Fetch a file name from a folder with current Date and Time using Java Code 我们如何在没有系统和Android服务器的情况下获得当前日期和时间(IST)? - How can we get current date and time (IST) without system and server in android? 我如何计算预定义日期时间和最近日期时间之间的秒数差异 - How can i calculate difference in seocnds between a predefined date time and recent date time 我如何使用currentTimeMillis()方法在Java中显示当前时间 - how can i display current time in java using currentTimeMillis() method 如何在不使用系统时区和系统时钟的情况下在 time4j 获得准确的 UTC 当前时间戳? - How can I get exact UTC current time stamp at time4j without usage of system timezone and system clock? 如何判断Java日期和时区是否在当前时间之前? - How can I tell if a Java Date and time zone is before the current time? 在不使用任何库的情况下在java中进行QRCode解码 - QRCode decoding in java without using any libraries 我想在 Java 中获取当前时间但没有 Internet 和系统时间 - I want to get current time in Java but without the Internet and without the system time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM