该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
折腾了快一天了,还不知道哪里出错了
package com.example.love;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txt2=null;
private TextView txt4=null;
private TextView txt5=null;
private Button btn1=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txt2=(TextView) super.findViewById(R.id.txt2);
this.txt4=(TextView) super.findViewById(R.id.txt4);
this.txt5=(TextView) super.findViewById(R.id.txt5);
this.btn1=(Button) super.findViewById(R.id.btn1);
txt2.setText(getdate());
txt4.setText(datesub());
txt5.setText(getTime());
}
public String getdate() {
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH) + 1;
int mDay = c.get(Calendar.DAY_OF_MONTH);
String time = mYear + "-" + mMonth + "-" + mDay;
return time;
}
private String datesub() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date0 = null;//新对象要初始化
try {
date0 = sdf.parse("2014-01-01");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date1 = null;
try {
date1 = sdf.parse("2014-01-05");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time0 = date0.getTime();
long time1 = date1.getTime();
String s = ""+(time1-time0)/1000/3600/24;
return s;
}
public String getTime() {
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH) + 1;
int mDay = c.get(Calendar.DAY_OF_MONTH);
int mHour = c.get(Calendar.HOUR_OF_DAY);
int mMinute = c.get(Calendar.MINUTE);
int mSecond = c.get(Calendar.SECOND);
String time = mYear + "-" + mMonth + "-" + mDay + " " +format(mHour) +":" + format(mMinute) + ":" + format(mSecond);
return time;
}
private String format(int x) {
String s = ""+x;
if(s.length() == 1)
s = "0"+s;
return s;
}
}