编译时与运行时
Compilation就像让工程师审查建筑师的建筑计划,然后写出更详细的规格.尚无建筑物.现在我们已经准备好进行建造,但是直到“运行时”施工人员到达现场才真正建造任何东西.
在这个比喻中,您的source code是建筑师的图纸.工程师更详细的规范是Java编译器发出的bytecode.运行您应用的字节码的JVM是施工人员要在现场进行的工作.
另一种思考方式:
>类是在编译时确定的.
>在运行时确定对象(实例).
java.time
另外,您永远不要使用Date类.该类以及Java最早版本中的其他旧式日期时间类都很糟糕,并且设计选择不多.多年前,现代的java.time类取代了它们.
Instant.now() // Capture current moment in UTC.
OffsetDateTime.now( ZoneOffset.UTC ) // Capture current moment in UTC.
ZonedDateTime.now( ZoneId.of( "Africa/Casablanca" ) ) // Capture current moment as seen though the wall-clock time used by the people of a particular region (a time zone).
LocalDate.of( 2018 , Month.JANUARY , 23 ) // A date-only value, without time-of-day and without time zone.