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