I have a number representing the number of nanoseconds since 12:00 a.m., January 1, 1904, universal time. I wish to instantiate a java.util.Date
object representing that date. How should I proceed?
You first need to convert your number representing nanoseconds to milliseconds.
Then for the given date string, get the total number of milliseconds since the unix time Epoch, and then add the number earlier converted to milliseconds to it.
Here's the working code:
String target = "1904/01/01 12:00 AM"; // Your given date string
long nanoseconds = ...; // nanoseconds since target time that you want to convert to java.util.Date
long millis = TimeUnit.MILLISECONDS.convert(nanoseconds, TimeUnit.NANOSECONDS);
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm aaa");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(target);
long newTimeInmillis = date.getTime() + millis;
Date date2 = new Date(newTimeInmillis);
System.out.println(date2);
Add an import java.util.concurrent.TimeUnit;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With