Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created August 14, 2020 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Audhil/00c47eb7f726b587447191a24f77a28f to your computer and use it in GitHub Desktop.
Save Audhil/00c47eb7f726b587447191a24f77a28f to your computer and use it in GitHub Desktop.
app to display - current time in different timezones - keeping this for reference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/txtGMTTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current" />
<TextView
android:id="@+id/txtCurrentTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
tools:text="txtCurrentTime" />
</LinearLayout>
<Spinner
android:id="@+id/availableID"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/timezone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="timeZone" />
<TextView
android:id="@+id/txtTimeZoneTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="txtTimeZoneTime" />
</LinearLayout>
</LinearLayout>
// based on http://www.theappguruz.com/blog/android-time-zone-demo
class DummyActivity : Activity() {
private var spinnerAvailableID: Spinner? = null
private var current: Calendar? = null
private var textTimeZone: TextView? = null
private var txtCurrentTime: TextView? = null
private var txtTimeZoneTime: TextView? = null
private var miliSeconds: Long = 0
private var idAdapter: ArrayAdapter<String>? = null
private var sdf: SimpleDateFormat? = null
private var resultdate: Date? = null
/**
* Called when the activity is first created.
*/
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dummy)
spinnerAvailableID = findViewById<View>(R.id.availableID) as Spinner
textTimeZone = findViewById<View>(R.id.timezone) as TextView
txtCurrentTime = findViewById<View>(R.id.txtCurrentTime) as TextView
txtTimeZoneTime = findViewById<View>(R.id.txtTimeZoneTime) as TextView
val idArray = TimeZone.getAvailableIDs()
sdf = SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss")
idAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, idArray)
idAdapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerAvailableID!!.adapter = idAdapter
getGMTTime()
spinnerAvailableID!!.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>,
view: View,
position: Int,
id: Long) {
getGMTTime()
val selectedId = parent.getItemAtPosition(position) as String
val timezone = TimeZone.getTimeZone(selectedId)
val timeZoneName = timezone.displayName
val timeZoneOffset = (timezone.rawOffset / (60 * 1000))
val hrs = timeZoneOffset / 60
val mins = timeZoneOffset % 60
miliSeconds += timezone.rawOffset
resultdate = Date(miliSeconds)
println("yup onItemSelected(): " + sdf!!.format(resultdate))
textTimeZone!!.text = ("$timeZoneName : GMT $hrs.$mins")
txtTimeZoneTime!!.text = "" + sdf!!.format(resultdate)
miliSeconds = 0
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}
}
// Convert Local Time into GMT time
// Convert Local Time into GMT time
private fun getGMTTime() {
current = Calendar.getInstance()
txtCurrentTime!!.text = "" + current!!.getTime()
miliSeconds = current!!.timeInMillis
val tzCurrent = current!!.timeZone
var offset = tzCurrent.rawOffset
if (tzCurrent.inDaylightTime(Date())) {
offset += tzCurrent.dstSavings
}
miliSeconds -= offset
resultdate = Date(miliSeconds)
println("yup getGMTTime(): " + sdf!!.format(resultdate))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment