tags:

views:

47

answers:

1

I do not know the reason why am i getting same values of different JSON date values. Here is my code for parsing date values in JSON date format:

package com.jsondate.parsing;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class JSONDateParsing extends Activity {
    /** Called when the activity is first created. */
    String myString;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        TextView tv = new TextView(this);
        //Date d = (new Date(1266029455L));
        //Date d = (new Date(1266312467L));
        Date d = (new Date(1266036226L));


        //String s = d.getDate() + "-" + d.getMonth() + "-" + d.getYear() + d.getHours() + d.getMinutes() + d.getSeconds();
       // SimpleDateFormat sdf=new SimpleDateFormat("yyyy MMM dd @ hh:mm aa");
        //Toast.makeText(this, d.toString(), Toast.LENGTH_SHORT);
        Log.e("Value:", d.toString());
        myString = d.toString();
        String []words = myString.split(" ");

        for(int i = 0;i < words.length; i++)
            Log.e("Value:", words[i]);

        myString = words[2] + "-" + words[1] + "-" + words[5] + " " + words[3];

        tv.setText(myString);
        setContentView(tv);


    }
}
+1  A: 

As far as I know, there is no standard way of representing a date in JSON. It looks as though what you are receiving is an integral value representing the number of seconds that have elapsed since the epoch of January 1, 1970, 00:00:00 GMT. This is somewhat different than what the Date(long date) constructor is expecting. The constructor is expecting milliseconds since the epoch. You need to multiply the values from the JSON by 1000 to use them correctly with Date.

long jsonDate = 1266036226L;
Date date = new Date(jsonDate * 1000);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
String stringDate = dateFormat.format(date);

Here are the different representations for the examples that you have given. Do they correspond with what you are expecting?

  • 1266029455 <==> 12-Feb-2010 09:50:55
  • 1266312467 <==> 16-Feb-2010 04:27:47
  • 1266036226 <==> 12-Feb-2010 11:43:46
  • 1266072180 <==> 13-Feb-2010 09:43:46

Note that the default behavior of SimpleDateFormat is to use the local time zone. In my case this is GMT-0500. A different time zone can be specified by calling the setTimeZone method.

Matthew T. Staebler
Thanks Aeth! Wonder how can we display AM or PM in the above format?
Maxood
As you had in your example code, you can just add `aa` to the pattern. For example, `"dd-MMM-yyyy hh:mm:ss aa"` will place `AM` or `PM` at the end of the string.
Matthew T. Staebler
I see!And what about 12 hour format because this format might be 24 hours ...isn't it(i.e. 3:00 PM will be 15:00)? I want 3:00 PM to display as 3:00 PM and not 15:00 PM.Thank you
Maxood
The pattern `hh` is 12-hour format. If you would like 24-hour format, use `HH`. See the [SimpleDateFormat documentation](http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html) for more information on all of the possible patterns.
Matthew T. Staebler