Java中,怎么将当前时间的milliSeconds转换为hour级别的milliSeconds?

发布于 2022-09-03 14:43:25 字数 141 浏览 18 评论 0

场景:
得到一个时间戳A:1473406568
对应的日期是B:2016/9/9 15:36:8
现在需要的是时间戳C:2016/9/9 15:00:00 对应的毫秒。

现在有大量的毫秒A数据,需要直接转为C中值,请问应该怎么处理?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妖妓 2022-09-10 14:43:25

那玩意叫时间戳

Date date = new Date(1473406568);
SimpleDateFormat formatter =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
System.out.println(formatter.format(date));

若只取毫秒,可以使用 Calendar

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1473406568);
calendar.get(Calendar.MILLISECOND)
人间☆小暴躁 2022-09-10 14:43:25
long currentTimeMillis = System.currentTimeMillis();
Log.d("test", currentTimeMillis + "");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH");
String format = dateFormat.format(currentTimeMillis);
try {
    Date date = dateFormat.parse(format);
    long time = date.getTime();
    Log.d("test", time + "");
} catch (ParseException e) {
    e.printStackTrace();
}
    
Log打印结果:
test: 1473410102814
test: 1473408000000
沫尐诺 2022-09-10 14:43:25

本小时的毫秒 = 时间戳 % 3600000
小时时间戳 = 时间戳 - 本小时的毫秒
注意时区问题

大海や 2022-09-10 14:43:25
long ms = 1473406568 * 1000L;
Calendar c = Calendar.getInstance();
c.setTimeInMillis(ms);
c.clear(Calendar.MINUTE);
c.clear(Calendar.SECOND);

System.out.println(c.getTimeInMillis() / 1000); // unix timestamp
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(c.getTime()));
小霸王臭丫头 2022-09-10 14:43:25
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(1473406568);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
System.out.println(calendar.getTimeInMillis());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文