B
B
blackCover2021-03-08 00:14:47
Java
blackCover, 2021-03-08 00:14:47

How does the Calendar class work in this code?

Hello.
Tell me how the Calendar class works in this code
public static Calendar getDate() {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(timestamp * 1000);
return date;
}

timstamp - time received from open weather map. By request I receive weather data for 5 days and write it to List. Judging by the response from the server, I get a list of elements for 5 days with an interval of 3 hours. I 'm trying to display the weather data on the screen in a loop with the condition
if (weatherDay.getDate().get(Calendar.HOUR_OF_DAY) == 15), in the response timstamp = 1615215600 at the time of 15:00, but I get a shift of 2 hours ahead, and the number of the month by 1 ago, and naturally I do not fall into this condition. How to be in this situation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-03-08
@blackCover

Good afternoon!
In the last question, you said that you solved the issue.
How to solve the problem with "Unable to create converter for class"?

On the virtual device, the time zone was set to GTM-0 in the settings, and I have +2. This solved the problem.

And even then I drew your attention to the fact that the time you already have is indicated in milliseconds.
public static Calendar getDate() {
Calendar date = Calendar.getInstance();
date.setTimeInMillis(timestamp * 1000);
return date;
}

And the setTimeInMillis setter takes the time in milliseconds. So why are you multiplying the timestamp by 1000?
No need to multiply by 1000. Otherwise, you get 53152 as the current year instead of 2021.
Here, for clarity:
public static void main(String[] args) {
    long timestamp = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
// Ваш код:
    calendar.setTimeInMillis(timestamp * 1000);
    System.out.println(calendar.getTime());
// Мой код:
    calendar.setTimeInMillis(timestamp);
    System.out.println(calendar.getTime());
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question