转换秒,日期时间字符串 [英] Converting seconds to date time String
问题描述
我有划时代的时间秒,并希望将其转换为日 - 月 - 年HH:MM
我曾尝试以下,但它给了我错误的值。
台历挂历= Calendar.getInstance();
calendar.setTimeInMillis(秒* 1000);
串dateString = calendar.get(Calendar.DAY_OF_WEEK)+,+ .......
以上code不正常我在做什么错在这里。
例如,如果秒= 1299671538
那么它产生的时间字符串周五,1969年12月12日
这是它应该显示错误周三,2011年3月9日
例如,如果秒= 1299671538那么它产生的时间字符串截至周五,1969年12月12日这是不对的,应该显示星期三,2011年3月9日
您有整数溢出。只要使用以下(通知L后1000恒):
台历挂历= Calendar.getInstance();
calendar.setTimeInMillis(秒* 1000L);
串dateString = calendar.get(Calendar.DAY_OF_WEEK)+,+ .......
或更好地利用的SimpleDateFormat类:
SimpleDateFormat的格式化=新的SimpleDateFormat(EEEE,MMMM研发,YYYY HH:MM);
字符串dateString = formatter.format(新日期(秒* 1000L));
这会给你以下日期字符串你原来秒输入:
周三,2011年3月9日13:52
I have seconds from epoch time and want to convert it to Day-Month-Year HH:MM
I have tried following but it gives me wrong value.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
Above code is not working properly am i doing anything wrong here.
For example if seconds = 1299671538
then it generates time string as Friday, December 12, 1969
which is wrong it should display Wednesday, March 09, 2011
For example if seconds = 1299671538 then it generates time string as Friday, December 12, 1969 which is wrong it should display Wednesday, March 09, 2011
You have integer overflow. Just use the following (notice "L" after 1000 constant):
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(seconds*1000L);
String dateString = calendar.get(Calendar.DAY_OF_WEEK) + ", "+.......
or better use SimpleDateFormat class:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
this will give you the following date string for your original seconds input:
Wednesday, March 9, 2011 13:52
这篇关于转换秒,日期时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!