当我运行程序时,系统当前时间显示在textview中。 现在我有一个按钮,一个edittext和一个textview。 我想当我在编辑文本上放40时,我按当前小时秒计算,然后通过在文本视图上按下按钮来显示它。 当我从edittext获取它时,我只想计算当前小时秒。 如您所知,当前小时秒数由一个数字计算,它也会影响分钟和小时
请帮助我 此代码显示当前时间: Thread myThread = null;
Runnable runnable = new CountDownRunner();
myThread= new Thread(runnable);
myThread.start();
}
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try{
txtCurrentTime= (TextView)findViewById(R.id.lbltime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
答案 0 :(得分:0)
当前时间:
txtCurrentTime= (TextView)findViewById(R.id.txtcurrunt);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":" + minutes + ":" + seconds;
txtCurrentTime.setText(curTime);
用户输入:
userInput=(EditText)findViewById(R.id.userInputInSeconds);
String userInput=userInput.getText().toString();
long increment=Long.parseLong(userInput);
//New Time
Date dt = new Date();
long newTime =dt.getTime()+(increment)/1000;
Date newData=new Date(newTime);
int hours = newData.getHours();
int minutes = newData.getMinutes();
int seconds = newData.getSeconds();
String newTimeString = hours + ":" + minutes + ":" + seconds;
txtNewTime= (TextView)findViewById(R.id.txtNew);
txtNewTime.setText(newTimeString);
我希望它会帮助你...... :)
答案 1 :(得分:0)
使用日历。您可以更轻松地将秒数添加到当前时间。
int seconds=Integer.parseInt(mTextView.getText()); // get textview's value (in integer)
Calendar cal=Calendar.getInstance();
int hours = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
String currentTimeString = String.format("%1$tH:%1$tM:%1$tS",cal);//24-hour format : HH:MM:SS
cal.add(Calendar.SECOND,seconds);
String newTimeString = String.format("%1$tH:%1$tM:%1$tS",cal);//24-hour format : HH:MM:SS
有关更多日期格式:请访问http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html