Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置UTC时区在Android中不起作用_Android_Timezone_Simpledateformat - Fatal编程技术网

设置UTC时区在Android中不起作用

设置UTC时区在Android中不起作用,android,timezone,simpledateformat,Android,Timezone,Simpledateformat,我正在尝试使用以下代码将毫秒时间值转换为UTC 12小时格式: public void updateDateAndTimeForMumbai(String value) { SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm"); SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");

我正在尝试使用以下代码将毫秒时间值转换为UTC 12小时格式:

    public void updateDateAndTimeForMumbai(String value) {
            SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm");
SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");

            TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
            // Create a calendar object that will convert the date and time value in milliseconds to date.
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

            try {
                calendar.setTimeInMillis(Long.parseLong(value));
                Log.i("Scheduled date: " + outputDateFormatter.format(calendar.getTime()));
                Log.i("Scheduled time: " + outputTimeFormatter.format(calendar.getTime()));
                Log.i("Scheduled time Am/Pm: " + new SimpleDateFormat("aa").format(calendar.getTime()));

            } catch (NumberFormatException n) {
                //do nothing and leave all fields as is

            }

        }
此处value=“1479633900000”


我不知道问题出在哪里。

您需要显式使用DateFormat.setTimeZone()以所需时区打印日期

outputDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
请在执行以下操作后调用此选项:

SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
如果从服务器接收的时间不是UTC时间,则不应将日历实例设置为UTC。但只需直接设置日历时间即可。
除去

打电话

Calendar calendar = Calendar.getInstance();  
以下是最终代码的外观:

public void updateDateAndTimeForMumbai(String value) {
            SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm");
            outputTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
            outputDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

            // Create a calendar object that will convert the date and time value in milliseconds to date.
            Calendar calendar = Calendar.getInstance();

            try {
                calendar.setTimeInMillis(Long.parseLong(value));
                Log.i("Scheduled date: " + outputDateFormatter.format(calendar.getTime()));
                Log.i("Scheduled time: " + outputTimeFormatter.format(calendar.getTime()));
                Log.i("Scheduled time Am/Pm: " + new SimpleDateFormat("aa").format(calendar.getTime()));

            } catch (NumberFormatException n) {
                //do nothing and leave all fields as is

            }

        }

它不起作用,我仍然得到同样的结果
Calendar calendar = Calendar.getInstance();  
public void updateDateAndTimeForMumbai(String value) {
            SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm");
            outputTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
            outputDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

            // Create a calendar object that will convert the date and time value in milliseconds to date.
            Calendar calendar = Calendar.getInstance();

            try {
                calendar.setTimeInMillis(Long.parseLong(value));
                Log.i("Scheduled date: " + outputDateFormatter.format(calendar.getTime()));
                Log.i("Scheduled time: " + outputTimeFormatter.format(calendar.getTime()));
                Log.i("Scheduled time Am/Pm: " + new SimpleDateFormat("aa").format(calendar.getTime()));

            } catch (NumberFormatException n) {
                //do nothing and leave all fields as is

            }

        }