Java – Android gets the date 7 days ago (one week)

How to get the date in android in this format within the previous week:

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

For example: 2010-09-19 HH: mm: SS, one week ago 2010-09-12 HH: mm: SS

thank you

Solution

Resolution date:

Date myDate = dateFormat.parse(dateString);

Then either calculate how many milliseconds you need to subtract:

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

Or use Java util. API provided by Calendar Class:

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

Then, if necessary, convert it back to string:

String date = dateFormat.format(newDate);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>