Why am i getting same values of different JSON date values?
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 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.
var d1 = ui.item.IssueDate;
var d = new Date(parseInt(d1.slice(6, -2)));
var Issdate = ("0" + (d.getMonth() + 1)).slice(-2) + '/' + ("0" + d.getDate()).slice(-2) + '/' + d.getFullYear().toString();
$('#IssueDate').val(Issdate);
精彩评论