时间相差8小时
使用 Date() 获取当前时间,有些手机(OPPO RS9)会差8小时,有些手机又不会。
使用 Calendar 获取当前时间正常。
环境:Android Studio 2.3.3
一、JAVA代码
public class MainActivity extends AppCompatActivity {
private TextView sTime1, sTime2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sTime1 = (TextView) findViewById(R.id.textView1);
sTime2 = (TextView) findViewById(R.id.textView2);
}
public void onGetTime(View v) {
String s1 = getMyDateTimeAdd8();
String s2 = getMyDateTime();
sTime1.setText(s1);
sTime2.setText(s2);
}
//使用 Date() 获取当前时间,有些手机(OPPO RS9)会差8小时,有些手机又不会。
public String getMyDateTimeAdd8() {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
long rightTime = (long) (d.getTime() + 8 * 60 * 60 * 1000); //加上8小时对应的毫秒数
String newtime = sd.format(rightTime);
return newtime;
}
// 使用 Calendar 获取当前时间就正常。
public String getMyDateTime() {
final Calendar mCalendar = Calendar.getInstance();
int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
int mMinuts = mCalendar.get(Calendar.MINUTE);
int mSend = mCalendar.get(Calendar.SECOND);
int mYear = mCalendar.get(Calendar.YEAR);
int mMonth = mCalendar.get(Calendar.MONTH) + 1;
int mDay = mCalendar.get(Calendar.DATE);
String newtime = String.format("%d-%02d-%02d %02d:%02d:%02d", mYear, mMonth, mDay, mHour, mMinuts, mSend);
return newtime;
}
}
二、布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ccc.cwh.csdn002.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onGetTime"
android:text="当前时间" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
使用 Date() 获取当前时间,有些手机(OPPO RS9)会差8小时,有些手机又不会。
使用 Calendar 获取当前时间正常。
环境:Android Studio 2.3.3
一、JAVA代码
public class MainActivity extends AppCompatActivity {
private TextView sTime1, sTime2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sTime1 = (TextView) findViewById(R.id.textView1);
sTime2 = (TextView) findViewById(R.id.textView2);
}
public void onGetTime(View v) {
String s1 = getMyDateTimeAdd8();
String s2 = getMyDateTime();
sTime1.setText(s1);
sTime2.setText(s2);
}
//使用 Date() 获取当前时间,有些手机(OPPO RS9)会差8小时,有些手机又不会。
public String getMyDateTimeAdd8() {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
long rightTime = (long) (d.getTime() + 8 * 60 * 60 * 1000); //加上8小时对应的毫秒数
String newtime = sd.format(rightTime);
return newtime;
}
// 使用 Calendar 获取当前时间就正常。
public String getMyDateTime() {
final Calendar mCalendar = Calendar.getInstance();
int mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
int mMinuts = mCalendar.get(Calendar.MINUTE);
int mSend = mCalendar.get(Calendar.SECOND);
int mYear = mCalendar.get(Calendar.YEAR);
int mMonth = mCalendar.get(Calendar.MONTH) + 1;
int mDay = mCalendar.get(Calendar.DATE);
String newtime = String.format("%d-%02d-%02d %02d:%02d:%02d", mYear, mMonth, mDay, mHour, mMinuts, mSend);
return newtime;
}
}
二、布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ccc.cwh.csdn002.MainActivity">
<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onGetTime"
android:text="当前时间" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>