Java Reference
In-Depth Information
// Format a Date and Time without timezone information
DateTimeFormatter nTZ =
DateTimeFormatter . ofPattern ( "d MMMM, yyyy h:mm a" );
System . out . println ( ZonedDateTime . now (). format ( nTZ ));
}
}
Converting Among Dates/Times, YMDHMS, and Epoch
Seconds
Problem
You need to convert among dates/times, YMDHMS, Epoch seconds, or some other numeric
value.
Solution
Use the appropriate Date/Time factory or retrieval methods.
Discussion
“The Epoch” is the beginning of time as far as modern operating systems go. Unix time, and
some versions of Windows time, count off inexorably the seconds since the Epoch. When
Ken Thompson and Dennis Ritchie came up with this format in 1970, seconds seemed like a
fine measure, and 32 bits' worth of seconds seemed nearly infinite. On systems that store this
in a 32-bit integer, however, time is running out. Older versions of most operating systems
stored this as a 32-bit signed integer, which unfortunately will overflow in the year 2038.
When Java first came out, it featured a method called System.currentTimeMillis() ,
presenting Epoch seconds with millisecond accuracy. The new Java API uses “epoch nano-
seconds,” which are still on the same time base.
Any of these epoch-related numbers can be converted into, or obtained from, a local date/
time. Other numbers can also be used, such as integer years, months, and days. As usual,
there are factory methods that create new objects where a change is requested. Here is a pro-
gram that shows some of these conversions in action:
DateConversions.java
Search WWH ::




Custom Search