-7

How to convert Object as timestamp to formatted date?

I have printed out timestamp(

1395500668

) that is in Object. I want to format this and print it out like

yyyy-MM-dd H:m:s

Jason C
  • 38,729
  • 14
  • 126
  • 182
HeiJai
  • 1
  • 1
  • 1
  • 4
  • No efforts ? Or at least share whatever you tried – Andy897 Mar 22 '14 at 16:00
  • As [@Andy897](http://stackoverflow.com/users/969107/andy897) pointed out, it would be good to show where you've looked for help so far and possibly what you have already tried. It seems this [SO question/answer](http://stackoverflow.com/questions/129181/formatting-timestamps-in-java) will give you what you are looking for. – hrbrmstr Mar 22 '14 at 16:18
  • @HeiJai You need to tell us what `Object` is, "the timestamp is in Object" doesn't really mean much. For all we know it could be anything (is it a `Timestamp`? a `String`? something else entirely?) – Jason C Mar 22 '14 at 16:30
  • Use "long" and "new date" return "null", the object get by json decode that's a timestamp. – HeiJai Mar 22 '14 at 16:32
  • @HeiJai How exactly are you decoding it from JSON and what does your input JSON look like? We're trying to help you, but you need to help *us* help you by giving enough information for us to know what is going on. – Jason C Mar 22 '14 at 16:36
  • @JasonC My JSON code : JSONObject json_Object = (JSONObject) new JSONParser().parse(json); json_time = json_Object.get("time"); – HeiJai Mar 22 '14 at 16:39
  • @HeiJai You do not seem to be using Jackson. Are you using GWT? Don't make us pull teeth here. Until you can give us a complete description of your problem, you will not get an answer here. All the tools you need to do this have been given in the answers below. Use your problem-solving skills to figure out how to get a `String` or a `long` containing your timestamp value from your JSON object. Consult the documentation for the JSON parser you are using for more information. – Jason C Mar 22 '14 at 16:42
  • @JasonC I'm using JSON-Simple to decode that get Object for me, Then I getted a timestamp, I want to format it to String with formatted date. – HeiJai Mar 22 '14 at 16:46
  • 3
    It looks like your question has been closed due to lack of sufficient information to diagnose the problem (i.e. because we're not psychic, and can't read your mind or see your code). If you'd like to see it reopened, you may wish to edit it to include a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates your problem. – Ilmari Karonen Mar 22 '14 at 16:59

3 Answers3

1

Assuming you have a string that represents epoch time in seconds (as you have not told us what your Object actually is), first convert it to a long:

long epoch = Long.parseLong("1395500668");

You'll then need to convert it to milliseconds:

epoch *= 1000;

Then you can convert it to a java.util.Date using the Date constructor that takes millisecond epoch time:

Date date = new Date(epoch);

And finally you can format that Date as a string using standard formatting techniques.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
1

First convert the timestamp value into Date and then format the date into your desired format using SimpleDateFormat

java.util.Date date = new java.util.Date(new Long(1395500668) * 1000);
String dateStr = new java.text.SimpleDateFormat("yyyy-MM-dd H:m:s").format(date);
System.out.println(dateStr);

It outputs:

2014-03-22 8:4:28
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
0

Convert it first into a Date first by casting the Object into a Timestamp and then using the getTime() method as shown in How to convert TimeStamp to Date in Java?

Then use a SimpleDateFormat to format the date as needed as shown in Change date format in a Java string

Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35