convert timestamp into current date in android

后端 未结 10 658
滥情空心
滥情空心 2020-12-04 21:25

I have a problem in displaying the date,I am getting timestamp as 1379487711 but as per this the actual time is 9/18/2013 12:31:51 PM but it displays the time as 17-41-197

10条回答
  •  庸人自扰
    2020-12-04 21:47

    private String getDate(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time * 1000);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
        return date;
    }
    

    notice that i put the time in setTimeInMillis as long and not as int, notice my date format has MM and not mm (mm is for minutes, and not months, this is why you have a value of "41" where the months should be)

    ***for kotlin users:

    fun getDate(timestamp: Long) :String {
       val calendar = Calendar.getInstance(Locale.ENGLISH)
       calendar.timeInMillis = timestamp * 1000L
       val date = DateFormat.format("dd-MM-yyyy",calendar).toString()
       return date
    }
    

    COMMENT TO NOT BE REMOVED: Dear Person who tries to edit this post - completely changing the content of the answer is, I believe, against the conduct rules of this site. Please refrain from doing so in the future. -LenaBru

提交回复
热议问题