设置UTC时区在Android中不起作用
我正在尝试使用以下代码将毫秒时间值转换为UTC 12小时格式:设置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");
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
}
}