Reputation: 15740
India Standard Time (IST) is 5:30 hours (5 hours 30 minutes) ahead of Greenwich Mean Time. We show it as GMT+5.5.
Is there any way to find this value (in above case, value = 5.5) by time zone?
In my android app, I can get device time zone. But how can I get this value using device time zone?
Upvotes: 3
Views: 1366
Reputation: 79550
how can a get this value using device time zone
ZonedDateTime.
now(
ZoneId.systemDefault() // Or: ZoneId.of("Asia/Kolkata")
)
.getOffset()
.toString()
See this code run live at IdeOne.com.
+05:30
Solution using java.time
, the modern API:
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
int seconds = zdt.getOffset().getTotalSeconds();
System.out.println(seconds);
// If required, convert it into hours
double hours = seconds / 3600.0;
System.out.println(hours);
}
}
Output:
19800
5.5
Learn more about java.time
, the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 1
Reputation: 15740
This is actually what i need.
double rawOffset = TimeZone.getDefault().getRawOffset();
rawOffset = rawOffset / (60 * 60 * 1000);
System.out.println("rawOffset : " + rawOffset);
Upvotes: -1
Reputation: 531
double timezoneval = 0.0;
Calendar cal = Calendar.getInstance();
cal.set(mn.Year(), mn.Month(), mn.Day(), mn.Hour(), mn.minute(),
(int) mn.second());
TimeZone tz = TimeZone.getDefault();
boolean t = tz.inDaylightTime(cal.getTime());
if (t)
timezoneval = ((TimeZone.getDefault().getRawOffset())
/ (60 * 60 * 1000D) + (TimeZone.getDefault()
.getDSTSavings() / (60 * 60 * 1000D)));
else
timezoneval = ((TimeZone.getDefault().getRawOffset()) / (60 * 60 * 1000D));
You can try this code. This will give you the value of time zone. If you want different timezone value; just change
TimeZone tz = TimeZone.getDefault();
to
TimeZone tz = TimeZone.getTimeZone("Asia/Kolkata");
TimeZone.setDefault(tz);
It will work for you.
Upvotes: 2
Reputation: 516
Check the Timezone class. The raw offset is what you want, in millisecond. You can easily convert it to value in hours.
long rawOffset = TimeZone.getDefault().getRawOffset();
See http://developer.android.com/reference/java/util/TimeZone.html#getRawOffset()
Upvotes: 1
Reputation: 931
try this code
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String dateStr = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String newDateStr = sdf.format(date);
System.out.println(newDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
output should like this
2010-06-14 08:21:49 AM
Upvotes: 0
Reputation: 82563
Try:
TimeZone.getDefault();
getDefault() returns the timezone details of the current device. You can then call getOffset() to get the difference between UTC and this time zone.
Upvotes: 0