简体   繁体   English

如何在UTC中显示所选时区的吐司

[英]How to Show Toast of selected TimeZone in UTC

I want to show the toast message of UTC time from the selected timezone from the spinner list shown in Image below. 我想显示下图所示的微调框列表中所选时区的UTC时间的敬酒消息。

在此处输入图片说明

below is my code: 下面是我的代码:

 Spinner tz;
 tz = (Spinner) findViewById(R.id.edit);


    String[] idArray = TimeZone.getAvailableIDs();
    ArrayAdapter<String> idAdapter = new ArrayAdapter<>(getApplicationContext(),R.layout.custom,idArray);
    idAdapter.setDropDownViewResource(R.layout.custom);
    tz.setAdapter(idAdapter);


    for(int i = 0; i < idAdapter.getCount(); i++) {
        if(idAdapter.getItem(i).equals(TimeZone.getDefault().getID())) {
             tz.setSelection(i);


        }
    }

I have tried this but cannot get my result 我已经尝试过了但是无法得到我的结果

http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName%28boolean http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName%28boolean

You must use setOnItemSelectedListener for this purpose. 为此 ,必须使用setOnItemSelectedListener An example usage is shown below. 用法示例如下所示。

tz.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

            SimpleDateFormat df = new SimpleDateFormat("dd MMM HH:mm a", Locale.getDefault());
            df.setTimeZone(TimeZone.getTimeZone(idAdapter.getItem(position))); //format in given timezone
            String strDate = df.format((new Date()).getTime());
            //Display the Toast
            Toast.makeText(getApplicationContext(), strDate, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

Note that onItemSelected method is also invoked when the view is being build, so may consider putting it inside onCreate() method call. 请注意,在构建视图时也会调用onItemSelected方法,因此可以考虑将其放入onCreate()方法调用中。

Hope this helps. 希望这可以帮助。

Not sure if I understand your question right, but: You are saying that you want to display the time of the different timezones in UTC? 不知道我的理解是否正确,但是:您是说要显示UTC中不同时区的时间吗? UTC is a timezone so the time in UTC will always be the same time for each of your selections. UTC是一个时区,因此每个选择中的UTC时间始终是相同的时间。 You probably want to display the local time of the selected timezone, and not UTC. 您可能要显示所选时区的本地时间,而不是UTC。

To do so use : System.currentTimeMillis() it gives you the number of milliseconds since January 1, 1970 00:00:00 UTC. 为此,请使用: System.currentTimeMillis()它提供自1970年1月1日UTC以来的毫秒数。

With DateFormat you can set your timezone and convert it to the local time. 使用DateFormat,您可以设置时区并将其转换为本地时间。

DateFormat df = DateFormat.getTimeInstance();
//Here set the timezone according to the selection in your spinner 
df.setTimeZone(TimeZone.getTimeZone("gmt")); 
String gmtTime = df.format(new Date());

Hope this helps Cheers 希望这对干杯有帮助

并制作吐司,使用

Toast.makeText(getApplicationContext(),gmtTime,Toast.LENGTH_SHORT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM