-2

I am getting API weather data from some weather website. It has provided me timezone as 19800 which I want to convert to GMT + 05:30 but I am not able to do the same.

Also it has given temperatures as 297.58 which I want to convert to deg Celcius.

I tried assuming that given temperature as deg Kelvin and reducing 273 from it but it is not working

I want to be able to convert seconds to time zone and show temp in degree celcius.

Linus
  • 39
  • 4
  • 3
    You seem to be asking two questions (convert offset in seconds to offset in hours, and converting degrees celsius to kelvin). Please [edit] it so that there is only one question. You can post your other question in another post. Also, you should show some code, as a [mcve] of the problem that you are having. – Sweeper Sep 19 '19 at 09:32
  • I too would have assumed that 297.58 were Kelvin. If subtracting 273 doesn’t work, I have no further suggestions. What exactly did you mean by *it is not working*? If you are getting an unexpected result, please tell us what you expected and how observed result differed. If you are getting an error message, quote verbatim. Thank you. – Ole V.V. Sep 20 '19 at 07:30
  • 1
    By not working I meant the outcome value was not the expected one. However the website mentioned that the the value is in Kelvin. I guess it was some mistake on my part. – Linus Sep 20 '19 at 18:32

2 Answers2

1

Offset conversion using java.time

It’s simple when you know how:

    int offsetSeconds = 19_800;
    ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSeconds);
    System.out.println("Offset is " + offset);

Output is:

Offset is +05:30

Next you may use the obtained ZoneOffset object to convert the date and time. The details depend on the format of your date and time.

Temperature conversion

I agree that your temperature looks very much like degrees Kelvin. If so, the formula given in the other answer by @Lt . Null is correct:

    double temperatureKelvin = 297.58;
    double temperatureCelsius = temperatureKelvin - 273.15;
    System.out.println("Temperature is " + temperatureCelsius + " degrees Celsius");

Temperature is 24.430000000000007 degrees Celsius

The many decimals come from floating point math most often being inaccurate. (For accuracy, use BigDecimal instead.) For output to the user you may want to print fewer decimals. If so, format the number using NumberFormat and/or DecimalFormat.

If 24.43 °C disagrees with your expectation, you need to tell us what you had expected instead, at the very least.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Is there any reason you have used 19_800 in the variable int offsetSeconds ? The number might vary as I am taking API data from weather sites so cannot stick to 19800. That number was just an example – Linus Sep 20 '19 at 18:26
  • I understood that. I took 19800 as an example, but since in my code example I chose to give it as a constant (literal) to leave out any doubt about what my code did in this particular example, I preferred to type it as `19_800` for readability. It makes no functional difference, and as you said, since you are getting the number from an API in your real code, you need not care. – Ole V.V. Sep 20 '19 at 19:27
0

For your timezone converter use this

 long unix_seconds = 19800; 
       Date date = new Date(unix_seconds*1000L); 
       // format of the date 
       SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
       formate.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
       String dateFinal = formate.format(date);
       System.out.println("\n"+dateFinal+"\n");

And finding kelvin to degree just use 297.58K − 273.15 = 24.43°C

Lt . Null
  • 1
  • 1
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 20 '19 at 07:41
  • Result is `1970-01-01 11:00:00 GMT+05:30`. The time zone is as requested, but otherwise I wonder if this was desired. Only the original questioner can tell. I took the freedom of using your temperature conversion formula in my answer too, hope you don’t mind. – Ole V.V. Sep 20 '19 at 07:52