convert timestamp into current date in android

后端 未结 10 592
滥情空心
滥情空心 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:00

    I got this from here: http://www.java2s.com/Code/Android/Date-Type/CreateDatefromtimestamp.htm

    None of the above answers worked for me.

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(Integer.parseInt(tripBookedTime) * 1000L);
    Date d = c.getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
    return sdf.format(d);
    

    And by the way: ("dd-MM-yyyy", cal) is not recognized by Android - "Cannot resolve method".

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-04 22:06

    If your result always returns 1970, try this way :

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

    You need to multiple your TS value by 1000

    It’s so easy to use it.

    0 讨论(0)
  • 2020-12-04 22:07
      DateFormat df = new SimpleDateFormat("HH:mm", Locale.US);
      final String time_chat_s = df.format(time_stamp_value);
    

    The time_stamp_value variable type is long

    With your code it will look something like so:

    private String getDate(long time_stamp_server) {
    
        SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
        return formatter.format(time_stamp_server);
    } 
    

    I change "milliseconds" to time_stamp_server. Consider changing the name of millisecond to "c" or to something more global. "c" is really good because it relates to time and to computing in much more global way than milliseconds does. So, you do not necessarily need a calendar object to convert and it should be as simple as that.

    0 讨论(0)
提交回复
热议问题