0

I am trying to format the milliseconds date from sample milliseconds value-1451646394000 to get the week number.

Following is the code snippet:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;

public class Test1 {
    public static void main(String[] args) {

        final String dateFormat = "YYYY-w";
        ZoneId zoneId_UTC = ZoneId.of("UTC");
        final long indexTimeStampMillis = 1463510726000L;
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(indexTimeStampMillis), zoneId_UTC);

        // Case 1:
        final DateTimeFormatter weekFormatter = DateTimeFormatter.ISO_WEEK_DATE;
        String output1 = dateTime.format(weekFormatter);
        System.out.println("Correct output of week number according to ISO Week numbers" + output1);

        // Case 2:
        final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withZone(zoneId_UTC)
                .withChronology(IsoChronology.INSTANCE);
        String output = dateTime.format(formatter);
        System.out.println("Week number I am getting in output " + output);

    }

}

Output on console:

Correct output of week number according to ISO Week numbers2016-W20-2

Week number I am getting in output 2016-21

NOTE: Correct week number for the above date is 20 according to ISO 8601. The date in milliseconds converts to Tue, 17 May 2016 18:45:26 GMT.

user4593933
  • 11
  • 1
  • 3
  • 1
    well... your code is so partial.. it's really hard to resolve ANY issue in that case – ymz Feb 16 '17 at 11:28
  • Thanks! Edited the above question, provided complete code snippet. – user4593933 Feb 16 '17 at 15:01
  • Great! Now that I understand your code - I do think that this shift depends on week deceleration - does your week starts on Sunday or Monday? – ymz Feb 16 '17 at 20:30
  • Won't both give week number according to ISO 8601 where week number starts from Monday? If January 1st is Monday then week 1 starts on January 1st and If January 1st is Tuesday then week 1 starts on December 31st of the previous standard year? – user4593933 Feb 17 '17 at 05:39
  • Apparently if you add day-of-week (`-e`) to your `dateFormat` pattern, it will return that day of week is `3` compared to ISO's `2`. So it is day of week thing, and my assumption is that since those results are locale-dependent, you will need to fix both locales to same value in both patterns for it to work as expected... except I tried that in fiddle, and nothing changed. – M. Prokhorov Feb 17 '17 at 16:48

0 Answers0