已知时间毫秒值,得到N天前的时间

1988_1989 2011-10-18 05:42:54

例如:已知了2011-10-3 15:12:30 的毫秒值,

要得到4天前9:00 的时间的毫秒值。

...全文
70 点赞 收藏 10
写回复
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozhuangfeng 2011-10-19
多熟悉下api就ok
回复
1988_1989 2011-10-19
public class MessageTimeUtil {

public static Long sendMessageTime(Long followTime, int day, int hour) {

Calendar c = Calendar.getInstance();
c.setTimeInMillis(followTime);
c.add(Calendar.DATE, -day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
System.out.println(c.getTime() + "------------" + c.getTimeInMillis());

return c.getTimeInMillis();
}



/** 测试 */
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
Long time = MessageTimeUtil.sendMessageTime(c1.getTimeInMillis(),20, 22);
System.out.println(time);
}
}
回复
1988_1989 2011-10-19
public class TimeUtil {

public static Long sendMessageTime(Long followTime, int day, int hour) {

Calendar c = Calendar.getInstance();
c.setTimeInMillis(followTime);
c.add(Calendar.DATE, -day);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
System.out.println(c.getTime() + "------------" + c.getTimeInMillis());

return c.getTimeInMillis();
}



/** 测试 */
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
Long time = MessageTimeUtil.sendMessageTime(c1.getTimeInMillis(),20, 22);
System.out.println(time);
}
}
回复
1988_1989 2011-10-19
[Quote=引用 6 楼 gukuitian 的回复:]

楼主同学自己查下Calendar的方法,一看就知道了。

有个setTimeInMillis的方法。
[/Quote]


恩,问题已经解决。。。
回复
gukuitian 2011-10-19
楼主同学自己查下Calendar的方法,一看就知道了。

有个setTimeInMillis的方法。
回复
1988_1989 2011-10-18
[Quote=引用 1 楼 gukuitian 的回复:]

Java code
Calendar c=Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.DATE, -4);
c.set(c.HOUR_OF_DAY, 9);
c.set(c.MINUTE, 0);
c.set(c.SECOND, ……
[/Quote]


如果用户选择的是2011-10-1
回复
1988_1989 2011-10-18
[Quote=引用 2 楼 jushi1988 的回复:]

引用 1 楼 gukuitian 的回复:
Java code
Calendar c=Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.DATE, -4);
c.set(c.HOUR_OF_DAY, 9);
c.set(c.MINUTE, 0);
c.set(c.SECO……


[/Quote]

日期是不确定的,用户自己选择,
回复
1988_1989 2011-10-18
[Quote=引用 1 楼 gukuitian 的回复:]

Java code
Calendar c=Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.DATE, -4);
c.set(c.HOUR_OF_DAY, 9);
c.set(c.MINUTE, 0);
c.set(c.SECOND, ……
[/Quote]
给值:
1316398647950 (毫秒值)得到相应时间
回复
LoongMint 2011-10-18
[Quote=引用 1 楼 gukuitian 的回复:]
Java code
Calendar c=Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.DATE, -4);
c.set(c.HOUR_OF_DAY, 9);
c.set(c.MINUTE, 0);
c.set(c.SECO……
[/Quote]
+1
回复
gukuitian 2011-10-18
Calendar c=Calendar.getInstance();
System.out.println(c.getTime());
c.add(c.DATE, -4);
c.set(c.HOUR_OF_DAY, 9);
c.set(c.MINUTE, 0);
c.set(c.SECOND, 0);
System.out.println(c.getTime()+"--"+c.getTimeInMillis());
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告