您的位置:首页 > 编程语言 > Java开发

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

2016-05-18 11:54 519 查看
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日期转换参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: