Dolphin
Dolphin

Reputation: 38835

why get the wrong week display name from unix timestamp in Java

Now I am using this Java code to get weeks display name:

String dateTime = CustomDatetimeUtil.timeStamp2Date(response.getStatisticTime(), "yyyy-MM-dd");
                response.setStatisticDate(dateTime);
                String displayName = Instant.ofEpochSecond(response.getStatisticTime())
                        .atZone(ZoneId.of("Asia/Shanghai"))
                        .getDayOfWeek()
                        .getDisplayName(TextStyle.FULL, Locale.CHINA);

the response.getStatisticTime() value is 1614783599999. But I get the display name is · Sunday(星期日). the correct value is wednesday. what should I do to get the right value?

enter image description here

Upvotes: 0

Views: 98

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501586

You're using Instant.ofEpochSecond- but the value you've got is large enough that it's presumably the number of milliseconds since the Unix epoch, not the number of seconds.

So basically you need to use Instant.ofEpochMilli instead.

I would recommend using the Epoch Converter site in future too - that allows you to put in a numeric value, and it will show you what that value means including the units it has assumed when performing the conversion.

Upvotes: 7

Related Questions