答案 0 :(得分:23)
使用java.util.Calendar,将其设置为今天的日期,然后减去7天。
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DAY_OF_YEAR, -7);
Date 7daysBeforeDate = cal.getTime();
编辑:在Java 8中,使用java.time包中的类可以更轻松地完成:
final LocalDate date = LocalDate.now();
final LocalDate dateMinus7Days = date.minusDays(7);
//Format and display date
final String formattedDate = dateMinus7Days.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(formattedDate);
答案 1 :(得分:5)
你可以试试这个,
import java.util.Calendar;
public class AddDaysToCurrentDate {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//add days to current date using Calendar.add method
now.add(Calendar.DATE,1);
System.out.println("date after one day : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
//substract days from current date using Calendar.add method
now = Calendar.getInstance();
now.add(Calendar.DATE, -10);
System.out.println("date before 10 days : " + (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.DATE)
+ "-"
+ now.get(Calendar.YEAR));
Typical output would be
Current date : 12-25-2007
date after one day : 12-26-2007
date before 10 days : 12-15-2007
答案 2 :(得分:3)
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();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
3 个答案:答案 0 :(得分:23)使用java.util.Calendar,将其设置为今天的日期,然后减去7天。Calendar cal = GregorianCalendar.getInstance();cal.setTime(new Date());cal.add(Calendar.DAY_OF_YEAR, -7);Date 7daysBeforeDate = cal.getTime();...
2017年5月14日
今
天
带孩子去玩了,所以时间有点紧,回来的时候给电脑更新驱动,结果崩了,只能重装系统,幸好是SSD盘,比较快,不过也耽误了时间,以后不能随便更新驱动了,切记。
今
天
看了点第2.4章。整个第2章后面阶段,觉得太深奥了,还需要后面再反复看了,争取明
天
看完第2章。
在做项目时有时会有需求要在指定时间段内进行特定操作,比如21点到第二
天
7点不进行响铃提醒,必须在5点到8点进行任务打卡等等,这时就需要进行时段
判断
,下面这段操作就是时段
判断
,可直接加入工具类中使用。
*
判断
当前
系统时间是否在特定时间的段内
* @param beginHour 开始的小时,例如5
* @param beginMin 开始小时的分钟数,例如00
* @param e...
@TargetApi(Build.VERSION_CODES.N)
protected String getTime(){
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SE
public boolean isLatestWeek(String timeStr) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date judgeTime = sdf...
public static boolean IsToday(String day) throws ParseException {
//
当前
的时刻
Calendar pre = Calendar.getInstance();
Date predate = new Date(System.currentTimeMillis());
pre.setTime(preda
```java
Locale currentLocale = getResources().getConfiguration().locale;
String currentLanguage = currentLocale.getLanguage();
这将返回
当前
设备的语言代码,例如“zh”表示中文,“en”表示英语等。你可以使用这个代码来
判断
当前
语言是否是你想要显示的语言。