I get from remote API such kind body:
{
"timezone": "18000",
"id": "1512569",
"name": "Tashkent"
}
How can I convert timezone field to GMT format? For example in my case:
timezon:18000 = GMT 5
You can convert the given number of seconds into a ZoneOffset
instance and then format it the desired string e.g.
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendLiteral("GMT")
.appendOffset("+H:mm", "Z")
.toFormatter(Locale.ROOT);
ZoneOffset offset = ZoneOffset.ofTotalSeconds(18000);
String formattedOffset = formatter.format(offset);
System.out.println(offset);
System.out.println(formattedOffset);
}
}
Output:
+05:00
GMT+5
Learn more about the the modern date-time API from Trail: Date Time.