怎么把安卓程序获取当前时间显示
1个回答
推荐于2016-01-12
展开全部
import java.util.Calendar;
import android.app.Activity;import android.os.Bundle;
public class GetSystemTime001Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
String time=c.get(Calendar.YEAR)+"-"+ //得到年
formatTime(c.get(Calendar.MONTH)+1)+"-"+//month加一 //月
formatTime(c.get(Calendar.DAY_OF_MONTH))+" "+ //日
formatTime(c.get(Calendar.HOUR_OF_DAY))+":"+ //时
formatTime(c.get(Calendar.MINUTE))+":"+ //分
formatTime(c.get(Calendar.SECOND)); //秒
System.out.println(time); //输出
}
private String formatTime(int t){
return t>=10? ""+t:"0"+t;//三元运算符 t>10时取 ""+t
}
}
import android.app.Activity;import android.os.Bundle;
public class GetSystemTime001Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
String time=c.get(Calendar.YEAR)+"-"+ //得到年
formatTime(c.get(Calendar.MONTH)+1)+"-"+//month加一 //月
formatTime(c.get(Calendar.DAY_OF_MONTH))+" "+ //日
formatTime(c.get(Calendar.HOUR_OF_DAY))+":"+ //时
formatTime(c.get(Calendar.MINUTE))+":"+ //分
formatTime(c.get(Calendar.SECOND)); //秒
System.out.println(time); //输出
}
private String formatTime(int t){
return t>=10? ""+t:"0"+t;//三元运算符 t>10时取 ""+t
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询