Parthi
Parthi

Reputation: 658

How to get the time zone of a place in android?

I have a problem in timezone showing. I need to show the time zone and time like "12/11/2014 11:45 IST". I can show the time. But I can't show the zone which place is PST, EST, or IST. How can I do it? Anyone can help me?

My source code is blelow.

    Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

Upvotes: 1

Views: 3294

Answers (5)

IlikHok
IlikHok

Reputation: 1

Here is an example of how you can determine that your application's time is in a different time zone than the system time zone. If yes - open the time settings

TimeZone.setDefault(null) // clear cached timezone
val localeCountry = Locale.getDefault().country // get default country 
code
val availableTimeZoneList = TimeZone.getAvailableIDs(localeCountry).toList() // available time zone list for default country code
val currentTimeZone = TimeZone.getDefault()

if(!availableTimeZoneList.contains(currentTimeZone.id) && availableTimeZoneList.all { TimeZone.getTimeZone(it).rawOffset != currentTimeZone.rawOffset }) {
    startActivity(Intent(android.provider.Settings.ACTION_DATE_SETTINGS))
}

Upvotes: 0

Coldfin Lab
Coldfin Lab

Reputation: 361

public static String getPSTDate() {
        String returnFormat = "";
        try {
            Date startTime = new Date();
            TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
            DateFormat formatter = DateFormat.getDateInstance();
            formatter.setTimeZone(pstTimeZone);
            returnFormat = formatter.format(startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnFormat;
    }

    public static String getPSTime() {
        String returnFormat = "";
        try {
            Date startTime = new Date();
            TimeZone pstTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
            DateFormat formatter = DateFormat.getTimeInstance();
            formatter.setTimeZone(pstTimeZone);
            returnFormat = formatter.format(startTime);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnFormat;
    }

Upvotes: 0

Arjun
Arjun

Reputation: 133

 public static String TimezoneUrl = "https://maps.googleapis.com/maps/api/timezone/json?";

 API_KEY="Your API service key";
 newurl = TimezoneUrl+"location="+myLatitude+","
 +myLongitude+"&timestamp="+System.currentTimeMillis() / 1000 + "&key=" + API_KEY;
 response = makeServiceCall(url, ServiceHandler.GET);

 jsonResponse = new JSONObject(response);
 timesone = jsonResponse.getString("timeZoneName");


 for (int i = 0; i < timesone.length(); i++) {
        if (Character.isUpperCase(timesone.charAt(i))) {
            char c = timesone.charAt(i);
            timezone = timezone + c;
    }
 }

  public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
 }


 public String makeServiceCall(String url, int method,
    List<NameValuePair> params) {
 try {
    // http client
    DefaultHttpClient httpClient = new DefaultHttpClient();

    //httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Custom user agent");
    HttpEntity httpEntity = null;
    HttpResponse httpResponse = null;

    // Checking http request method type
     if (method == GET) {
        // appending params to url
        if (params != null) {
            String paramString = URLEncodedUtils    .format(params, "utf-8");
            url += "?" + paramString;
        }
        HttpGet httpGet = new HttpGet(url);

        httpResponse = httpClient.execute(httpGet);

    }
    httpEntity = httpResponse.getEntity();
    response = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

return response;

}

Result will give you IST as you expect Timezone.

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

You can use SimpleDateFormat to format your Date with TimeZone in simple way. As example, to show date with TimeZone as 12/11/2014 11:45 IST, you can use dd/MM/yyyy HH:mm z format as below where z will represent the TimeZone as like EST, IST.

Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm z");
String formatedDate = dateFormat.format(cal.getTime());

Upvotes: 0

Ganesh Katikar
Ganesh Katikar

Reputation: 2690

Have you use TimeZone.getDefault(): Most applications will use TimeZone.getDefault() which returns a TimeZone based on the time zone where the program is running.

For more info: http://developer.android.com/reference/java/util/TimeZone.html

Try below code also:

TimeZone tz = TimeZone.getDefault();
System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " tz.getID());

Upvotes: 1

Related Questions