-1

I want to convert Long value to String or Date in this format. I got this value in Long format: 2343805819061 which I got from Firebase timestamp. When ever I am trying I got the error that cant use Date into string:

    //constrictor
    public Wall_post( Date time_stamp) {

        this.time_stamp = time_stamp;

    }
    public Date getTime_stamp() {
        return time_stamp;
    }

    public void setTime_stamp(Date time_stamp) {
        this.time_stamp = time_stamp;
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

3 Answers3

0

using

   public static Date millisToDate(long millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return calendar.getTime();
    }
GianhTran
  • 3,443
  • 2
  • 22
  • 42
0

To get the timestamp in a String format, please use the following method:

public static String getTimeDate(long timeStamp){
    try{
        DateFormat dateFormat = getDateTimeInstance();
        Date netDate = (new Date(timeStamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "time";
    }
}

If you want only the date, then just use the following method:

public static String getDate(long timeStamp){
    try{
        DateFormat dateFormat = getDateInstance();
        Date netDate = (new Date(timeStamp));
        return dateFormat.format(netDate);
    } catch(Exception e) {
        return "date";
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

now use this code & let me know if it solves your problem

 //date converter
        String dateInToString = DateFormat.format("EEE, d MMM yyyy HH:mm:ss", new Date(time)).toString();

you can find more date formate details from bellow link SampleDateFormate || Android

ayan chakraborty
  • 284
  • 3
  • 14