convert timestamp into current date in android

后端 未结 10 590
滥情空心
滥情空心 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 22:01

    current date and time:

     private String getDateTime() {
            Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
            Long time = System.currentTimeMillis();
            calendar.setTimeInMillis(time);
    
           //dd=day, MM=month, yyyy=year, hh=hour, mm=minute, ss=second.
    
            String date = DateFormat.format("dd-MM-yyyy hh:mm:ss",calendar).toString();
            return date;
        }
    

    Note: If your result always returns 1970, try this way :

    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    calender.setTimeInMillis(time * 1000L);
    String date = DateFormat.format("dd-MM-yyyy hh:mm:ss", calendar).toString();
    

提交回复
热议问题