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);
}
}
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 withDate.Here are the different representations for the examples that you have given. Do they correspond with what you are expecting?
Note that the default behavior of
SimpleDateFormatis to use the local time zone. In my case this is GMT-0500. A different time zone can be specified by calling thesetTimeZonemethod.