tags:

views:

84

answers:

2

Hello , i'm trying to retrieve the current date via unix time like this:

   long unixTime = System.currentTimeMillis()/1000;

    Date d = new Date(unixTime);
    StringBuffer tmp = new StringBuffer();
    tmp.append(d.getYear());
    tmp.append(" - ");
    tmp.append(d.getMonth());
    tmp.append(" - ");
    tmp.append(d.getDay());`

When i later print this out via tmp.getString() i get the following date 70 - 0 - 4 , is there something im missing ?

//Thx in advance

A: 

Date takes milliseconds since epoch. So just don't divide unixTime by 1000.

bhups
+2  A: 

Those methods you are using are deprecated. You better use the GregorianCalendar class:

GregorianCalendar gregorianCalendar=new GregorianCalendar();            
StringBuffer tmp = new StringBuffer();
tmp.append(gregorianCalendar.get(GregorianCalendar.YEAR));
tmp.append(" - ");
tmp.append(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));
tmp.append(" - ");
tmp.append(gregorianCalendar.get(GregorianCalendar.MONTH));
Cristian
thx but i have a minor problem , GregorianCalendar.MONTH gives me the wrong value by a month, is there any quick fix to this ?
Krewie
i just incremented it by 1 but i dunno if its a good fix tbh, it could be that GregorianCalendar.MONTH counts january as 0 ? .
Krewie
Yes... it's a zero-indexed result.
Cristian