0
嗨,所有即时通讯有问题将日期和时间转换为毫秒可以任何人请帮助我它代码中没有错误,但它返回的错误日期和时间以毫秒为单位android returing时间戳以毫秒为单位
这是即时通讯转换日期:2011-03-01 17:55:15
及其给我这个号:-679019461843345152
,这是使用的代码IM:
public long getDate(String s){
//this is returning a timestamp but the wrong ones!!!
String[] formats = new String[] {
// "yyyy-MM-dd",
"yyyy-MM-dd HH:mm:ss"
// "yyyy-MM-dd HH:mmZ",
//"yyyy-MM-dd HH:mm:ss.SSSZ",
};
SimpleDateFormat sdf=null;
String st;
for (String format : formats) {
sdf = new SimpleDateFormat(format, Locale.US);
//System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
st = new String(sdf.format(new Date(0)));
System.err.format(format, st);
}
// compute nanoseconds from y, m...
//return that number
Calendar c = Calendar.getInstance();
c.set(sdf.YEAR_FIELD, sdf.MONTH_FIELD, sdf.DATE_FIELD, sdf.HOUR0_FIELD, sdf.MINUTE_FIELD, sdf.SECOND_FIELD);
return c.getTimeInMillis() * 1000000;
}
非常感谢,但即时通讯有一个小问题,当我添加了什么你发送到我的代码它告诉我一个错误它说这个日期的未处理的异常类型ParseException dt = sdf.parse(s) – moe 2011-04-11 03:41:12
通常每函数解析文本将需要在try-catch块内,以处理意外的字符串。我会更新答案,向您展示一个例子。 – Aleadam 2011-04-11 03:44:34
好吧,我得到了相同的结果,你得到了,并感谢你的帮助很多,但我有1最后一个问题,你进入的时间是17:55:15你得到的结果是我得到的是12:55 :15 .... ???? – moe 2011-04-11 04:51:30