According to the Javadoc, getMtimeString()
returns a String representation of the modification time, and getMTime()
returns the number of seconds since January 1, 1970 (the epoch), as an int
.
Java typically takes a date as either some string to be parsed, or a long
number of milliseconds since the epoch, or, in the case of java.time.LocalDate
, it can take the number of days since the epoch.
Since getMTime()
returns the number of seconds since the epoch, and there are 86400 seconds in a day, you can simply use it instead of getMtimeString()
and create a LocalDate
from it:
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
import java.time.LocalDate;
public class DaysToDate {
public static void main(String[] args) {
// ... whatever you need to do to get sftpChannel ...
int lastModif = sftpChannel.lstat(remoteFile).getMTime();
long days = lastModif / 86400L;
LocalDate date = LocalDate.ofEpochDay(days);
System.out.println(date.format(ISO_LOCAL_DATE));
System.out.println(date);
}
}
I have used a DateTimeFormatter
to format the date (there is a predefined formatter for the format you specified named ISO_LOCAL_TIME
), but that is the same as the default format for LocalDate
, so you can just call its toString()
method. If you want to format it differently, just create a DateTimeFormatter
with the desired format.
Note: Since getMTime()
represents the number of seconds since January 1, 1970 as a 32-bit int
, it suffers from the 2038 problem.