1

Problem:

I have got a directory of files, which have a creation date.

What I am trying to reach is to get the value of the creation date day, month and year.

However, I am working with a FileTime.

I was expecting to be able to call a GetMonth method or such.

Unfortunately, this is not possible, does someone know a nice solution to get the day/month/year of a FileTime?

What have I tried:

  • I have tried to convert this to the Date type. This is possible, but here are the day, month and year methods deprecated.

  • I have tried to use to get the milliseconds of the FileTime, but this did not feel like a pretty solution.

Final question: How do I get the day, month and year of a FileTime?

Thanks in forward.

zerk
  • 516
  • 4
  • 9
  • 34

1 Answers1

5

FileTimeInstantZonedDateTime

I'm assuming that you're using Java 8 or later. You must convert the FileTime to an Instant. You can then apply a ZoneId to the Instant to get a ZonedDateTime object for the time zone you want the date in. The below code converts the FileTime to date in India time.

val zonedTime = fileTime.toInstant().atZone(ZoneId.of("Asia/Kolkata"))
println(zonedTime.year)
println(zonedTime.monthValue)
println(zonedTime.dayOfMonth)
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Chandra Sekar
  • 10,683
  • 3
  • 39
  • 54