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.