Artem
Artem

Reputation: 4639

How convert timestamp to date on android?

I want convert timestamp to date and set time in this day to 12.00. How can I do this?

If I use this:

Date date = new Date(event.getActionDate()*1000);

I can't set hour or minutes to this date, becouse methods for this operations are deprecated.

Explanation:

I have timestamp -

1461924872 // Fri, 29 Apr 2016 10:14:32 GMT

I want change hour in this timestamp (10:14 to 00.00).

Upvotes: 2

Views: 2047

Answers (3)

Damini Mehra
Damini Mehra

Reputation: 3347

private String getDate(String timeStampStr){

try{
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date netDate = (new Date(Long.parseLong(timeStampStr)));
    return sdf.format(netDate);
}
catch(Exception ex){
    return dateInStr;
}
    }

let me know if it is working?

Upvotes: 1

Dhaval Patel
Dhaval Patel

Reputation: 10299

long timeInMillis = 1461877200000l;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
System.out.println("Date1:"+calendar.getTime().toString());

calendar.set(Calendar.HOUR_OF_DAY, 0);  //For 12 AM use 0 and for 12 PM use 12.  
calendar.set(Calendar.MINUTE, 0);       
calendar.set(Calendar.SECOND, 0);

Date date = calendar.getTime(); 
System.out.println("Date2:"+date.toString());

Upvotes: 3

marj
marj

Reputation: 23

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
date = sdf.parse(localTime);

Upvotes: 1

Related Questions