Java Reference
In-Depth Information
Again, the English translation can be read as, “The contents of the field
mill
are
represented in milliseconds. Convert those contents into hours. Next, convert those
hours into days.”
TimeUnit
can make time interval conversion very precise. Combining the preci-
sion of the
TimeUnit
conversions along with mathematics will allow you to convert
the difference of two dates into just about any time interval.
4-16. Obtaining Date-Time from a Speci-
fied String
Problem
You want to parse a string into a date-time object.
Solution
Utilize the
parse()
method of a temporal date-time class to parse a string using a
predefined or custom format. The following lines of code demonstrate how to parse a
string into a date or date-time object using variations of the
parse()
method.
// Parse a string to form a Date-Time object
LocalDate ld = LocalDate.parse("2014-12-28");
LocalDateTime ldt
= LocalDateTime.parse("2014-12-28T08:44:00");
System.out.println("Parsed Date: " + ld);
System.out.println("Parsed Date-Time: " + ldt);
// Using a different Parser
LocalDate ld2 = LocalDate.parse("2014-12-28",
DateTimeFormatter.ISO_DATE);
System.out.println("Different Parser: " + ld2);
// Custom Parser
String input = "12/28/2013";
try {