0

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 );
Aldridge1991
  • 1,326
  • 6
  • 24
  • 51
  • 1
    "I do everything fine" - apparently not, but it's hard to say *what* you're doing wrong when we can't see your code. Please show us everything relevant. – Jon Skeet Aug 30 '12 at 20:31
  • Your code is 'right' maybe you should look at `Calendar.getInstance();` Quote: "Returns: A Calendar subclass instance set to the current date and time in the default Timezone." Timezone messed up? – Nate-Wilkins Aug 30 '12 at 20:47

3 Answers3

1

why don't you use DateFormat

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
view2.setText(dateFormat.format(c.getTime());
ColdFire
  • 6,764
  • 6
  • 35
  • 51
1

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!

Community
  • 1
  • 1
GKlesczewski
  • 192
  • 4
0

You can instead use this,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String currentDateandTime = sdf.format(new Date())
Rob
  • 415,655
  • 72
  • 787
  • 1,044
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41