[英]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.Date
和java.util.Calendar
的源代码,您可以直接从http://jdk7.java.net/source.html下载源代码,也可以在docjar.com
或此处在线查看源代码。 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.