问题描述
- Android Studio获取GMT+8时间出错?
-
package com.example.thread; import java.util.Timer; import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.format.Time; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.text.format.DateFormat; //import java.util.Timer; import android.text.format.Time; import android.widget.TextView; public class MainActivity extends Activity { public TextView time_text; public String text2="9"; public int year,month,date,hour,minute,second; public Handler mHandler=new Handler(); Time t=new Time("GMT+8"); // or Time t=new Time("GMT+8"); 加上Time Zone资料。 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); time_text=(TextView)findViewById(R.id.time_text); time_text.setTextColor(Color.WHITE); //Time t=new Time("GMT+8"); // or Time t=new Time("GMT+8"); 加上Time Zone资料。 mHandler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub t.setToNow(); // 取得系统时间。 year = t.year;//年 month = t.month;//月 date = t.monthDay;//日 hour = t.hour; //小时 minute = t.minute;//分钟 second = t.second;//秒 //输出当前时间 时分秒 time_text.setText(String.valueOf(" " + hour + ":" + minute + ":" + second)); mHandler.postDelayed(this, 1000); } }); } }
我想要显示东八区时间,可是app却显示中时区时间,怎么回事?
我是在android4.1.2版本手机上运行app的。我的手机系统自带的时间显示是东八区时间,app显示却不一样。
解决方案
public static String getLocalDatetimeString(String local) {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(local));
cal.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
String date = cal.get(Calendar.YEAR) +"-"+ (cal.get(Calendar.MONTH) + 1) +"-"+ cal.get(Calendar.DAY_OF_MONTH);
String time = cal.get(Calendar.HOUR_OF_DAY) +":"+ cal.get(Calendar.MINUTE) +":"+ cal.get(Calendar.SECOND);
return date +""+ time;
}
然后这样调用
getLocalDatetimeString("GMT+8");
祝你好运!
解决方案二:
获取GMT8时间
本地时间(北京时间)和“GMT+8”时区时间的区别?
解决方案三:
楼上暖枫无敌说的有道理。
public class MainActivity extends Activity
{
public TextView time_text;
public Handler mHandler=new Handler();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time_text=(TextView)findViewById(R.id.time_text);
time_text.setText(getLocalDatetimeString("GMT+8"));
time_text.setTextColor(Color.WHITE);
time_text.setText(" " + getLocalDatetimeString("GMT+8"));//显示时间
}
public static String getLocalDatetimeString(String local) {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(local));
cal.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
String date = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
String time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
return date + " " + time;
}
}
这样就能显示当前时间了。
时间: 2024-10-31 08:51:52