How to Get TimeZone From Your Mobile In Android



Here The Java and Xml Files for Getting the TimeZone

1). activity_main.xml file code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidtutorials.gettimezone.MainActivity">



    <Button
        android:id="@+id/timeZoneButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Time Zone"

        />
    <TextView
        android:id="@+id/timeZoneShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_below="@+id/timeZoneButton"
        android:layout_marginTop="20dp"
        android:layout_centerInParent="true"
        android:textSize="20sp"
        />

</RelativeLayout>


2). MainActivity.java file

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {

    Button TimeZoneButton;
    TextView TimeZoneTextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TimeZoneButton= (Button) findViewById(R.id.timeZoneButton);

        TimeZoneTextview= (TextView) findViewById(R.id.timeZoneShow);

        TimeZoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Use TimeZone Class for get Time Zone

                TimeZone tz=TimeZone.getDefault();

                TimeZoneTextview.setText("Time Zone : " + tz.getDisplayName(false,TimeZone.SHORT) + " \n TimeZone Id: "+ tz.getID());


            }
        });


    }
}

Watch Video --- Go to Android Video Tutorials









Comments

Popular Posts