我是Android开发的新手。请帮我解决这个问题。
我想在我的计数器中访问系统时间,即setText
。至于现在我使用了一些随机的日期和时间,但我希望将其更改为当前日期和时间。
以下是代码:
package countdowntimer.android;
import java.util.Date;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MActivity extends Activity {
TextView day,hour,min,sec;
int iDay,iHour,iMin,iSec;
MyCount counter;
Date endDate = null;
Date startDate = null;
NumberFormat myFormat = NumberFormat.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
day=(TextView)findViewById(R.id.day);
hour=(TextView)findViewById(R.id.hour);
min=(TextView)findViewById(R.id.min);
sec=(TextView)findViewById(R.id.sec);
myFormat.setMinimumIntegerDigits(2);
final EditText end=(EditText)findViewById(R.id.end);
final EditText start=(EditText)findViewById(R.id.start);
start.setText("2011-09-08-00:00:00");
Button btnStart=(Button)findViewById(R.id.btnstart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
try {
endDate = outputFormat.parse(end.getText().toString());
startDate=outputFormat.parse(start.getText().toString());
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
/*Toast.makeText(getBaseContext(), ex.getMessage()
,Toast.LENGTH_SHORT).show();*/
}
long diffInMis= endDate.getTime() - startDate.getTime();
if(diffInMis<0){
Toast.makeText(getBaseContext(), "Please, Enter valid Time..."
,Toast.LENGTH_SHORT).show();
}
else{
long diff = TimeUnit.MILLISECONDS.toSeconds(diffInMis);
iDay=(int) (diff/(60*60*24));
long lday= (diff%(60*60*24));
iHour=(int)(lday/3600);
long lhour= (lday%(60*60));
iMin=(int)(lhour/60);
long lmin= (lhour%(60));
iSec=(int)(lmin);
day.setText(String.valueOf(iDay).toString()+" Day ");
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
sec.setText(":"+String.valueOf(myFormat.format(iSec)).toString());
counter = new MyCount(iSec*1000,1000);
counter.start();}
} catch (ParseException e) {
e.printStackTrace();
} }; });
Button btnStop=(Button)findViewById(R.id.btnstop);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
counter.cancel();
} }); }
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter = new MyCount(60000,1000);
counter.start();
iMin-=1;
if(iMin>-1)
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
else{
iMin=59;
min.setText(":"+String.valueOf(myFormat.format(iMin)).toString());
iHour-=1;
if(iHour>-1)
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
else{
iHour=11;
hour.setText(String.valueOf(myFormat.format(iHour)).toString());
iDay-=1;
if(iDay>-1)
day.setText(" "+String.valueOf(iDay).toString());
else{
day.setText("Time is over");
hour.setText("");
min.setText("");
sec.setText("");
counter.cancel();
}}}}
@Override
public void onTick(long millisUntilFinished) {
sec.setText(":"+String.valueOf(myFormat.format(millisUntilFinished/1000)));
}}}
答案 0 :(得分:3)
您可以使用
访问系统时间Calendar.getInstance();
int hh = cal.get(Calendar.HOUR_OF_DAY);//or
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String aux[] = sdf.format(cal.getTime()).split("/");//process the array from here
请注意,您必须
import java.util.Calendar;