1

当我运行程序时,系统当前时间显示在文本视图中。现在我有一个按钮、一个edittext 和一个textview。我想当我在edittext上放40时,我按当前小时秒数计算,然后通过在textview上按下按钮来显示它。当我从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);
4

2 回答 2

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);

我希望它会帮助你... :)

于 2012-08-04T16:14:05.303 回答
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

于 2012-08-06T05:47:08.903 回答