2010-09-27 5 views

Répondre

7

Essayez cette DateFormat.getDateFormat(mContext).format(new Date(myTimestamp * 1000))

Comme new Date() nécessite millisecondes au lieu de secondes, vous avez à plusieurs par 1000

+0

Vérifie maintenant .. – Curtain

+0

Merci! Ça a marché. – Curtain

+0

Thx, ça a marché. –

2

Vous devez convertir l'horodatage milisecond à long int en ajoutant un L dans la multiplication:

DateFormat.getDateFormat(mContext).format(new Date(myTimestamp * 1000L)) 
3

Vous pouvez également avoir un contrôle plus strict du format final en le rendant explicite. Il permet également d'éviter la création d'un objet Date et n'a pas besoin d'contexte:

DateFormat.format("dd/MM/yyyy hh:mm:ssaa", myTimeStamp * 1000L); 
1
/** 
* Get the human readable String representation of time 
* @param unix_time 
* @return String format of time eg 13:20 
* @throws NullPointerException 
* @throws IllegalArgumentException 
*/ 
public String convertFromUnix(String unix_time) throws NullPointerException, IllegalArgumentException{ 
    long time = Long.valueOf(unix_time); 
    String result = ""; 
    Date date = new Date(time); 
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); 
    format.setTimeZone(TimeZone.getTimeZone("GMT")); 
    result = format.format(date); 
    Log.d("date", format.format(date)); 

    return result; 
} 
Questions connexes