-1

value of nanosseconds 130016196641685504

Expected Result : Wednesday, January 2, 2013 5:01:04pm

Output from this code : 1974-02-14 01:06:36

   FileTime time = FileTime.from(nanoSeconds,TimeUnit.NANOSECONDS);
   String pattern = "yyyy-MM-dd HH:mm:ss";
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
   Date date = new Date(time.toMillis());
   System.out.println(simpleDateFormat.format(date));

To be more specific, I am trying to convert the nanoseconds to humanReadable format in java. Refer this link for more details...

thinktoday
  • 29
  • 1
  • 8
  • 1
    I have closed as a duplicate. When I insert your value of 130016196641685504 into the code in that original question and answer, I get `2013-01-02T17:01:04.168550400Z`, which seems to agree with what you expected. – Ole V.V. May 26 '20 at 20:11
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `Instant`, `DateTimeFormatter` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 26 '20 at 20:12
  • @OleV.V. Thanks, It works fine thanks.... – thinktoday May 27 '20 at 05:14

2 Answers2

0

Why do you expect Wednesday, January 2, 2013 5:01:04pm?

Read the specifications

Parameters:
value - the value since the epoch (1970-01-01T00:00:00Z); can be negative

https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/FileTime.html#from-long-java.util.concurrent.TimeUnit-

And 130016196641685504 is about 4 years.

So your expectations are wrong.

Jocke
  • 2,189
  • 1
  • 16
  • 24
  • https://www.silisoftware.com/tools/date.php?inputdate=130016196641685504&inputformat=filetime refer this link , how this conversion works? – thinktoday May 26 '20 at 19:22
  • But you are using the java FileTime.from library, READ that specification. It is like you are using a map for Chicago in NYC. – Jocke May 26 '20 at 19:26
  • Thank you, Is there any way to do the above mentioned conversion in java? – thinktoday May 26 '20 at 19:30
  • Not without knowing how silisoftware has implemented their converted. But why do you think 130016196641685504 should give you Wednesday, January 2, 2013 5:01:04pm That is like if I told you that 10 + 30 is 42, and you then try to get java to calculate that. You will not be able to do it using standard libraries since it is telling lies. But you could most likely reverse engineer it yourself... – Jocke May 26 '20 at 19:36
0

tl;dr

Your input is an encoded value, per comment by Ole V.V. and covered in original Question. Not a count of nanos.

Multiply by ten for a count of nanoseconds

If you have a count of groups of 100 nanoseconds, then multiply by ten (10) to get a count of nanoseconds.

long input = 130_016_196_641_685_504L ;
long nanos = input * 10 ;

Convert to an Instant.

Instant instant = Instant.EPOCH.plus( nanos , ChronoUnit.NANOS ) ;

instant.toString(): 2011-03-15T04:06:06.416855040Z

You example input of 130_016_196_641_685_504L multiplied by ten representing a count of nanoseconds since the epoch reference of first moment of 1970 UTC results in 2011-03-15T04:06:06.416855040Z.

Your expectations are not valid

I suspect your expectations are incorrect. You should edit your Question to explain the reason for your expectation.

Your expected result of 2013-02-14T17:01:04Z is actually this count of nanoseconds since first moment of 1970 UTC: 1360861264000000000.

LocalDate ld = LocalDate.of( 2013 , Month.FEBRUARY , 14 ) ;
LocalTime lt = LocalTime.of( 17 , 1 , 4 ) ;
OffsetDateTime odt = OffsetDateTime.of( ld , lt , ZoneOffset.UTC ) ;
long output = Duration.between( Instant.EPOCH , odt.toInstant() ).toNanos() ;

See similar code run live at IdeOne.com.

Perhaps you assume incorrectly the epoch reference used by the publisher of your input data. Dozens of various epoch references are in use by various kinds of information systems.

➥ Or more likely, your input is not a count of nanos from epoch, but an encoded value as commented by Ole V.V., to be decoded by code seen on the linked Question.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • that will still not make it Wednesday, January 2, 2013 5:01:04pm – Jocke May 26 '20 at 19:05
  • Hii thanks for reply, https://www.silisoftware.com/tools/date.php?inputdate=130016196641685504&inputformat=filetime refer this link , how this conversion works? – thinktoday May 26 '20 at 19:21
  • That is not related to your question. Ask https://www.silisoftware.com how should we know that! For all we know they can have a own data time library implemented in their own language. What we do know for sure is that they don't use FileTime.from() from java – Jocke May 26 '20 at 19:30