I want to display the time in some Country, and the TimeZone is GMT+4.
private void loadWeather(){
TimeZone tz = TimeZone.getTimeZone("GMT+0400");
Calendar cal = Calendar.getInstance(tz);
Date date = cal.getTime();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT, Locale.getDefault());
String myDate = df.format(date);
tv_time.setText(myDate);
}
I've tried this, but it gives me my time, and not the other one
The problem is that you're specifying the time zone just on the Calendar
- which is only used to get the current instant in time, which doesn't depend on the time zone. You need to specify it on the format instead, so that it's applied when creating an appropriate text representation of that instant:
private void loadWeather() {
Date date = new Date(); // This is enough; it uses the current instant.
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("GMT+0400"));
String myDate = df.format(date);
tv_time.setText(myDate);
}
Or to inline even more:
private void loadWeather() {
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("GMT+0400"));
tv_time.setText(df.format(new Date()));
}
(This is assuming you really do want the short date/time format using the current locale.)
See more on this question at Stackoverflow