gpt4 book ai didi

java - Android 获取日期前 7 天(一周)

转载 作者:IT老高 更新时间:2023-10-28 20:50:32 27 4
gpt4 key购买 nike

如何以这种格式在android中获取一周前的日期:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

例如:现在 2010-09-19 HH:mm:ss,一周前 2010-09-12 HH:mm:ss

谢谢

最佳答案

解析日期:

Date myDate = dateFormat.parse(dateString);

然后要么算出你需要减去多少毫秒:

Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000

或者使用java.util.Calendar类提供的API:

Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();

然后,如果需要,将其转换回字符串:

String date = dateFormat.format(newDate);

关于java - Android 获取日期前 7 天(一周),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3747490/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com