4

Good day, I have timestamp 1481709600 and I would like to have this time format Wed, 14 Dec 2016

I'm trying to do that using:

private String getDateFromTimeStamp(Integer dt) {
            Date date = new Date (dt);
            return new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy ").format(date);
}

but the current output is Sun Jan 18 05:35:09 GMT+02:00 1970

I think the date format is wrong, which one I need to use ?

Thank you!

Update

the problem is that the year day and month is wrong, It should be 14 Dec 2016 instead of Jan 18 1970

sushildlh
  • 8,986
  • 4
  • 33
  • 77
VLeonovs
  • 2,193
  • 5
  • 24
  • 44

6 Answers6

7

Problem is your timeStamp is in seconds , so convert your timeStamp into millisec and then use the date format function ...

try this one ...

JAVA

 private String getDate(long time) {
        Date date = new Date(time*1000L); // *1000 is to convert seconds to milliseconds
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
        sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
    
        return sdf.format(date);;
    }

Kotlin

fun getDate(time:Long):String {
            var date:Date = Date(time*1000L); // *1000 is to convert seconds to milliseconds
            var sdf:SimpleDateFormat  = SimpleDateFormat("EEE, dd MMM yyyy "); // the format of your date
            sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
    
        return sdf.format(date);
    }

Output:- like this Wed, 14 Dec 2016

Note:- EEE is represent as Day in Week ,MMM is represent as Month in words and so on ..

sushildlh
  • 8,986
  • 4
  • 33
  • 77
2

You can make any combination if you read this
You need to use EEE, d MMM yyyy

Update

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy");
sdf.format(new Date(1481709600));
Salmaan
  • 3,543
  • 8
  • 33
  • 59
0

the dateformat is wrong; you have to use the

 new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").format(date);
gauss
  • 63
  • 1
  • 11
0

Try this method to get data. Put the time in setTimeInMillis as long and not as int

private String getDate(long time) {
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(time);
    String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
    return date;
}
Deepak Sachdeva
  • 950
  • 8
  • 16
0

try this

private String getDateFromTimeStamp(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time);
        String date = DateFormat.format("EEE MMM dd hh:mm:ss yyyy ", cal).toString();
        return date;
    }
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
  • @VLeonovs what is the timestamp you are passing. the mistake that you have done is , you have used `Integer` value as the parameter which should be `long` datatype that is y u where getting 1970 as the year, try the code above and revert if you are getting same output – SaravInfern Dec 14 '16 at 09:53
0

This works for me:

val currentDay = Date()
    val day = currentDay.time
    Logger.i("currentDay = $currentDay and day : $day ")

    val c = Calendar.getInstance()
    c.timeInMillis = day
    val d = c.time
    val sdf = SimpleDateFormat("dd/MM/yyyy HH:mm")

    Logger.i("meDate : ${sdf.format(d)}")
t j
  • 7,026
  • 12
  • 46
  • 66
Gama_aide
  • 61
  • 4