Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert GMT offset value to +/- HH:MM

TimeZone timeZone = TimeZone.getTimeZone("Asia/Calcutta");

    int offset1 = timeZone.getRawOffset();
    System.out.println("offset = "+ offset);

    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offsetHrs="+ offsetHrs);
    System.out.println("offsetMins="+ offsetMins);


    GMTOffset = offsetHrs + ":" + offsetMins;
    System.out.println("GMTOffset = " + utzOffset);

The out put will be

offset = 19800000

offsetHrs=5

offsetMins=30

GMT offset = 5:30

i am using this offset value in ms sql server to convert the time zone. sql server will takes the below format

(+/-)HH:MM

my result is 5:30 but it's giving an invalid timezone error it should be +5:30 .

in another case

TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT");

in this case offset will be "0"

but ms sql takes only +00:00 or -00:00 (+/- sign must)

can any one help me how to format the offset in (+/-)HH:MM format.

like image 757
Ravi Avatar asked Jan 27 '26 03:01

Ravi


1 Answers

You could use

int offset = timeZone.getRawOffset();
String gmtTZ = String.format("%s%02d:%02d", 
               offset < 0 ? "-" : "+", 
               Math.abs(offset) / 3600000,
               Math.abs(offset) / 60000 % 60);
like image 189
Peter Lawrey Avatar answered Jan 28 '26 15:01

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!