《JAVA语言程序设计》(四)|JAVA 获得当前系统时间

来源:互联网 发布:雅思培训网络课程 编辑:程序博客网 时间:2024/06/06 05:50
package com.example.calculatecurrenttime;import java.util.Calendar;import java.util.TimeZone;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class MainActivity extends Activity {    //程序运行在车机端,获得车机上的时间;程序运行在手机端,获取手机上的时间    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Calendar c = Calendar.getInstance();        TimeZone timeZone = c.getTimeZone();        // 或者        // TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");        long totalMilliseconds = System.currentTimeMillis() + timeZone.getRawOffset();        long totalSeconds = totalMilliseconds / 1000;        long currentSenconds = totalSeconds % 60;        long totalMinutes = totalSeconds / 60;        long currentMinutes = totalMinutes % 60;        long totalHours = totalMinutes / 60;        long currentHours = totalHours % 24;        Log.d("xiaoxi", "current time is :" + currentHours + ":" + currentMinutes + ":" + currentSenconds);    }}

Android日期转换参考

0 0
原创粉丝点击