0

Im using File.getlastmodified() to get last modifies of files but for some reason (probably UIT) its printing wrong date and month (year is fine)

Im getting last modifed with the above method and saving it to Long

//This should ideally conver the last modified to human radable format but its returning wrong month adn date

like----> 27/11/2018

shows as---> 28/10/2018

c = Calendar.getInstance(); 

Long epoch = files[i].lastModified();

epoch+=32400L;

c.setTimeInMillis(epoch); //im converting the milliseconds to human readable formate

int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH);
int day=c.get(Calendar.DATE);

Harish
  • 1,433
  • 9
  • 21
  • 3
    Month values go from `0` to `11`, so getting `10` for 11th month is correct behaviour. But date being wrong is kinda interesting, would your `+32400L` affect the day? Maybe the correct time is very close to midnight? – buræquete Jul 31 '19 at 05:16
  • You can try like this new SimpleDateFormat("dd/mmm/yyyy").format(file.lastModified())) – Sambit Jul 31 '19 at 05:18
  • 2
    @Sambit:The pattern should be `dd/MM/yyyy` – second Jul 31 '19 at 05:20
  • @sambit i fixed month with sdf but date is still incorrect for 27-11-2018 its coming as 28-11-2018 – Ashwin Satyawan Jul 31 '19 at 05:23
  • @Second, you are right. – Sambit Jul 31 '19 at 05:23
  • What is date is still off, I did not get . – Sambit Jul 31 '19 at 05:24
  • 2
    @AshwinSatyawan it must be due to either your assumed date is wrong, maybe on your local OS you see with different timezone than the java environment, or your adding extra milliseconds to the `epoch` is affecting the date. Try to check your java timezone with `System.out.println(TimeZone.getDefault());`, and check the timezone of environment where you got that assumed correct date. – buræquete Jul 31 '19 at 05:25
  • You didn't intend to add 9 hours, did you? You added 32.4 seconds, and I am asking because I don't understand why you did. – Ole V.V. Jul 31 '19 at 06:05
  • You may want to document your problem better. That will allow us to help ypu debug. Edit your question to include a file listing from the OS showing the correct modification time, and the value of `epoch` in your program. Also in what time zone do you want the date, and which is the default time zone of your JVM? – Ole V.V. Jul 31 '19 at 06:20

1 Answers1

5

The value is not incorrect. You are using the Calendar incorrectly. Checking the documentation you can see the value returned for Month.

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Source

Now, for the day, I would suspect the addition of 32 seconds could be the problem. But most probably the timezone. Indeed, the method return a value on GMT.


Note that I have check with a file modified the "2019-04-17 14:52:13" and get the correct result. Also, you can format the Calendar using a SimpleDateFormat instance instead of extracting the value like this.

private static String formatEpoch(long epoch) {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(epoch));
}

Or, we can never mention this enough, using a more recent API for date with Instant.

private static String formatEpoch(long epoch) {
    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
    return formatter.format(Instant.ofEpochMilli(epoch).atZone(ZoneId.systemDefault()));
}

An instant is a date-time at GMT, so we add the locale timezone before formatting it with a DateTimeFormatter to provide a value like :

2019-04-17T14:52:13.118
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • 3
    I warmly recommend the more recent API mentioned near the bottom of this answer. The old classes — `Calendar`, `SimpleDateFormat` and `Date` — were poorly designed, no reason to use them anymore. – Ole V.V. Jul 31 '19 at 06:02