android开发 怎么获取手机当前时间
3个回答
展开全部
Android的文件有建议用Time代替Calendar。用Time对CPU的负荷会较小。在写Widget时特别重要。
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
package itokit.com;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class ShowTime extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Time time = new Time("GMT+8");
time.setToNow();
int year = time.year;
int month = time.month;
int day = time.monthDay;
int minute = time.minute;
int hour = time.hour;
int sec = time.second;
myTextView.setText("当前时间为:" + year +
"年 " + month +
"月 " + day +
"日 " + hour +
"时 " + minute +
"分 " + sec +
"秒");
}
}
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
package itokit.com;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class ShowTime extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Time time = new Time("GMT+8");
time.setToNow();
int year = time.year;
int month = time.month;
int day = time.monthDay;
int minute = time.minute;
int hour = time.hour;
int sec = time.second;
myTextView.setText("当前时间为:" + year +
"年 " + month +
"月 " + day +
"日 " + hour +
"时 " + minute +
"分 " + sec +
"秒");
}
}
展开全部
Android的文件有建议用Time代替Calendar。用Time对CPU的负荷会较小。在写Widget时特别重要。
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
package itokit.com;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class ShowTime extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Time time = new Time("GMT+8");
time.setToNow();
int year = time.year;
int month = time.month;
int day = time.monthDay;
int minute = time.minute;
int hour = time.hour;
int sec = time.second;
myTextView.setText("当前时间为:" + year +
"年 " + month +
"月 " + day +
"日 " + hour +
"时 " + minute +
"分 " + sec +
"秒");
}
}
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
package itokit.com;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class ShowTime extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
Time time = new Time("GMT+8");
time.setToNow();
int year = time.year;
int month = time.month;
int day = time.monthDay;
int minute = time.minute;
int hour = time.hour;
int sec = time.second;
myTextView.setText("当前时间为:" + year +
"年 " + month +
"月 " + day +
"日 " + hour +
"时 " + minute +
"分 " + sec +
"秒");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = format.format(new java.util.Date());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询