Reputation: 1367
I've a problem when displaying the date. I do everything fine, the date shows...bad it's not correct!
It says 2012/07/30 when it should be 2012/08/30.
I've checked my date in the mobile and it's correct. Do you have any idea?
Thank you!!
Piece of code:
Calendar c = Calendar.getInstance();
String sDate = c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR);
view2.setText("" + et.getText() + sDate );
Upvotes: 0
Views: 93
Reputation: 3111
You can instead use this,
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String currentDateandTime = sdf.format(new Date())
Upvotes: 0
Reputation: 302
The Calendar object does not work as you are expecting it. The get(Calendar.MONTH) returns a constant representing the month of the date, not the number of the month. This subtle difference can be seen here: java.util.calendar
As you will see, the constants reflect a zero-based sequence, starting with January, so that you can use the constants as indexers into an array for month-based lookups.
For your purposes, you could get away with adding 1 to the returned int, but you might want to look at SimpleDateFormat in the java.text. This article provides a brief overview to get you started.
Good luck!
Upvotes: 1
Reputation: 6849
why don't you use DateFormat
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
view2.setText(dateFormat.format(c.getTime());
Upvotes: 1