THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this tutorial is to help you to get Time according to the Different Time Zone.

 

You will get Final Output:

  • Time Zone Demo
  • Time Zone Demo
  • Time Zone Demo
 

Step 1 MainActivity.java

First create new Android Project. Add Following into your Main Activity class

private Spinner spinnerAvailableID;
private Calendar current;
private TextView textTimeZone, txtCurrentTime, txtTimeZoneTime;
private long miliSeconds;
private ArrayAdapter<String> idAdapter;
private SimpleDateFormat sdf;
private Date resultdate;

Then in our Main Activity class we need all IDs of the different Time Zone. So we use.

String[] idArray = TimeZone.getAvailableIDs();
 

Step 2 getGMTTime() method implementation

Add new method for converting the current time into GMT (Greenwich Mean Time).

private void getGMTTime() {
current = Calendar.getInstance();
txtCurrentTime.setText("" + current.getTime());
 
miliSeconds = current.getTimeInMillis();
 
TimeZone tzCurrent = current.getTimeZone();
int offset = tzCurrent.getRawOffset();
if (tzCurrent.inDaylightTime(new Date())) {
offset = offset + tzCurrent.getDSTSavings();
}
 
miliSeconds = miliSeconds - offset;
 
resultdate = new Date(miliSeconds);
System.out.println(sdf.format(resultdate));
}

To know more about Calendar class please follow the link:

 

Step 3 onCreate() Method

Then Add the following code into your onCreate() method.

        spinnerAvailableID = (Spinner) findViewById(R.id.availableID);
textTimeZone = (TextView) findViewById(R.id.timezone);
txtCurrentTime = (TextView) findViewById(R.id.txtCurrentTime);
txtTimeZoneTime = (TextView) findViewById(R.id.txtTimeZoneTime);
 
sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss");
 
idAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, idArray);
 
idAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerAvailableID.setAdapter(idAdapter);
 
getGMTTime();
 
spinnerAvailableID
.setOnItemSelectedListener(new OnItemSelectedListener() {
 
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
getGMTTime();
String selectedId = (String) (parent
.getItemAtPosition(position));
 
TimeZone timezone = TimeZone.getTimeZone(selectedId);
String TimeZoneName = timezone.getDisplayName();
 
int TimeZoneOffset = timezone.getRawOffset()
/ (60 * 1000);
 
int hrs = TimeZoneOffset / 60;
int mins = TimeZoneOffset % 60;
 
miliSeconds = miliSeconds + timezone.getRawOffset();
 
resultdate = new Date(miliSeconds);
System.out.println(sdf.format(resultdate));
 
textTimeZone.setText(TimeZoneName + " : GMT " + hrs + "."
+ mins);
txtTimeZoneTime.setText("" + sdf.format(resultdate));
miliSeconds = 0;
}
 
@Override
public void onNothingSelected(AdapterView<?> arg0) {
 
}
});

More information about SimpleDateFormat class please follow the link:

I hope you enjoy this tutorial and it would be helpful to you.

Got an Idea of Android App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Android App Development Company in India.

An entrepreneur who has founded 2 flourishing software firms in 7 years, Tejas is keen to understand everything about gaming - from the business dynamics to awesome designs to gamer psychology. As the founder-CEO of a company that has released some very successful games, he knows a thing or two about gaming. He shares his knowledge through blogs and talks that he gets invited to.

face mask Belial The Demon Headgear Pocket Staff Magic