Hiho could someone tell me why these Code doesn't work right?
private Calendar dateTime = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eintragen);
this.dh = new DataHelper(this);
EditText Datum = (EditText) findViewById(R.id.et_meerwasser_wasserwerte_eintragen_datum);
Datum.setText(dateTime.get(Calendar.DAY_OF_MONTH)+"."+dateTime.get(Calendar.MONTH)+"."+dateTime.get(Calendar.YEAR));
}
Each time i start the app it tells me: 03.7.2011 But the actuall date is 03.08.2011
I restarted the mobilephone and taked off the battery
With kind re开发者_运维百科gards Alex
This is because Month will be considered from 0 and not from 1. Try modifying your code like this,
private Calendar dateTime = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eintragen);
this.dh = new DataHelper(this);
EditText Datum = (EditText) findViewById(R.id.et_meerwasser_wasserwerte_eintragen_datum);
Datum.setText(dateTime.get(Calendar.DAY_OF_MONTH)+"."+dateTime.get((Calendar.MONTH)+1)+"."+dateTime.get(Calendar.YEAR));
}
So you have to increment the value of your month by one to get the current month.
Add +1 with month.Because month starts from 0,not 1 in this Calendar.So for January you will get 0 rather than 1.
Datum.setText(dateTime.get(Calendar.DAY_OF_MONTH)+"."+(dateTime.get(Calendar.MONTH)+1)+"."+dateTime.get(Calendar.YEAR));
}
精彩评论