11

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 problem is in my activity I stored this value in a string variable because handler returning in string format).

2 Answers 2

10

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Long.parseLong(yourTimeString)
3

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;
}

2 Comments

Watch out when using the Calender class as it is slower than the Time class. Also concatenating a string that way is not very efficient. Try to use a StringBuilder instead.
@2red13 what is the add_string function? I agree with @Octavian Damiean, maybe a StringBuilder would be more efficient?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.