在android中将时间戳转换为字符串到日期
我从服务器获取的时间戳值为1291204449,所以我通过 实现处理程序 现在我想将时间戳值转换为日期。(但问题是在我的活动中,我将此值存储在字符串变量中,因为处理程序以字符串格式返回)。只需使用该类即可。尝试类似的方法在android中将时间戳转换为字符串到日期,android,timestamp,Android,Timestamp,我从服务器获取的时间戳值为1291204449,所以我通过 实现处理程序 现在我想将时间戳值转换为日期。(但问题是在我的活动中,我将此值存储在字符串变量中,因为处理程序以字符串格式返回)。只需使用该类即可。尝试类似的方法 Time time = new Time(); time.set(Long.valueOf(yourTimeString)); 如果你真的需要一个对象,试试这个 Date date = new Date(Long.parse(yourTimeString)); 我有一个将时
Time time = new Time();
time.set(Long.valueOf(yourTimeString));
如果你真的需要一个对象,试试这个
Date date = new Date(Long.parse(yourTimeString));
我有一个将时间戳转换为字符串格式的函数:
public static String create_datestring(String timestring){
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(timestring));
timestring = add_string(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)),"0",2,0) + "." + add_string(String.valueOf(cal.get(Calendar.MONTH)+1),"0",2,0) + "." + String.valueOf(cal.get(Calendar.YEAR)) + " " + add_string(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)),"0",2,0) + ":" + add_string(String.valueOf(cal.get(Calendar.MINUTE)),"0",2,0);
return timestring;
}
使用
日历
类时要小心,因为它比时间
类慢。用这种方式连接字符串也不是很有效。尝试改用StringBuilder
。@2red13添加字符串的函数是什么?我同意@Octavian Damiean的观点,也许StringBuilder会更有效?Long.parseLong(yourTimeString)