android convert";createAt";迄今为止
我有呼叫服务器,返回createAt的int,我想转换为dateandroid convert";createAt";迄今为止,android,Android,我有呼叫服务器,返回createAt的int,我想转换为date @JsonProperty(FoursquareWebService.WS_API_JSON_FIELD_CREATEDAT) public int getCreatedAt() { return this.createdAt; } 你能帮我吗 SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy");
@JsonProperty(FoursquareWebService.WS_API_JSON_FIELD_CREATEDAT)
public int getCreatedAt() {
return this.createdAt;
}
你能帮我吗
SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy");
String date = format.format(new Date(item.getCreatedAt()));
Log.e(">>>>>>>>>>CreateAt",
"" + item.getCreatedAt());
Log.e(">>>>>>>>>>",
"" + date);
结果:
05-28 10:22:01.829 10593-10593/com.cc E/>>>>>>>>>>CreateAt﹕ 1363880768
05-28 10:22:01.829 10593-10593/com.cc E/>>>>>>>>>>﹕ Jan 16, 1970
这是正确的:2012年10月3日尝试将您变成的字符串转换为如下日期:
String dtStart = item.getCreatedAt();
SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); //this is the format you become your date in
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
编辑:将毫秒转换为日期:
public static String getDate(long milliSeconds, String dateFormat){
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
然后你可以像这样调用这个方法:
System.out.println(getDate(item.getCreatedAt(), "MMM d, yyyy"));
首先,创建一个getDate()方法
此方法将接受长参数。在使用此方法之前,请将日期转换为long
int dateFromJson = item.getCreatedAt();
String s = Integer.toString(dateFromJson);
// Convert to milliseconds
long timestamp = Long.parseLong(s) * 1000;
Log.d("DATE", getDate(timestamp));
这表明您的日期为2013年3月21日。根据您要转换的内容,这是正确的结果,但您已经在转换日期。有什么问题吗?结果不正确(抱歉,但调试:W/System.err)﹕ java.text.ParseException:Unparseable date:“1334766899”(偏移量10处)ops,我的失败-我以为您的日期是开头的格式-查看我对postresult:I/System.out的编辑﹕ 1970年1月16日,但结果正确:2012年10月3日。你能帮我吗?事实上,你的代码有其他问题-以毫秒为单位的这个值正好返回1970年1月16日,你自己在这个在线转换器中尝试一下,并在代码中搜索一个异常,在那里你成为日期你有其他方法成为应该转换的日期吗我从服务器上打电话。你们可以看到我的编辑问题。
int dateFromJson = item.getCreatedAt();
String s = Integer.toString(dateFromJson);
// Convert to milliseconds
long timestamp = Long.parseLong(s) * 1000;
Log.d("DATE", getDate(timestamp));