Converting Timestamp as String to Date in android

前端 未结 2 1528
滥情空心
滥情空心 2020-12-20 14:41

I am getting Timestamp value as 1291204449 from server so I extracted the value by implementing handler.

Now I want to convert timestamp value to date.(But the prob

相关标签:
2条回答
  • 2020-12-20 15:39

    Just use the Time class. Try something similar to this.

    Time time = new Time();
    time.set(Long.valueOf(yourTimeString));
    

    If you really need a Date object just try this.

    Date date = new Date(Long.parse(yourTimeString));
    
    0 讨论(0)
  • 2020-12-20 15:41

    I have a function for converting timestamps in String Format:

    public static String create_datestring(String timestring){
        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(Long.parseLong(timestring));
        timestring = add_string(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)),"0",2,0) + "." +  add_string(String.valueOf(cal.get(Calendar.MONTH)+1),"0",2,0) + "." + String.valueOf(cal.get(Calendar.YEAR)) + " " + add_string(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)),"0",2,0) + ":" + add_string(String.valueOf(cal.get(Calendar.MINUTE)),"0",2,0);
        return timestring;
    }
    
    0 讨论(0)
提交回复
热议问题