2012-07-11 106 views
90

以下函数产生今天的日期;我怎么才能让它只产生昨天的日期?使用日期获取昨天的日期

private String toDate() { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     Date date = new Date();  
     return dateFormat.format(date).toString(); 
} 

这是输出:

2012-07-10 

我只需要昨天的日期如下图所示。有没有可能在我的功能做到这一点?

2012-07-09 

回答

238

你减去错误号:

使用Calendar代替:

private Date yesterday() { 
    final Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.DATE, -1); 
    return cal.getTime(); 
} 

然后修改方法如下:

private String getYesterdayDateString() { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     return dateFormat.format(yesterday()); 
} 

+0

此代码在Wed Jul 11​​ 04:21:43 GMT 2012格式中返回日期。但我想2012-7-11,请帮助我如何从给定的输出中提取此。 – Jaydipsinh 2013-01-05 09:19:32

+8

使用'yyyy-MM-dd'作为格式 – 2013-01-06 00:36:09

+0

对昨天()方法的单元测试会是什么? – prime 2018-02-19 07:20:42

1
Calendar cal = Calendar.getInstance(); 
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
    System.out.println("Today's date is "+dateFormat.format(cal.getTime())); 

    cal.add(Calendar.DATE, -1); 
    System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime())); 
1

没有直接的函数来获取昨天的日期。

要得到昨天的日期,您需要使用Calendar减去-1

2

从您的代码更改:

private String toDate(long timestamp) { 
    Date date = new Date (timestamp * 1000 - 24 * 60 * 60 * 1000); 
    return new SimpleDateFormat("yyyy-MM-dd").format(date).toString(); 

}

但你更好的使用calendar

8
Calendar cal = Calendar.getInstance(); 
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
    System.out.println("Today's date is "+dateFormat.format(cal.getTime())); 

    cal.add(Calendar.DATE, -1); 
    System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime())); 

使用日历阿比

6

试试这个:

private String toDate() { 
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 

    // Create a calendar object with today date. Calendar is in java.util pakage. 
    Calendar calendar = Calendar.getInstance(); 

    // Move calendar to yesterday 
    calendar.add(Calendar.DATE, -1); 

    // Get current date of calendar which point to the yesterday now 
    Date yesterday = calendar.getTime(); 

    return dateFormat.format(yesterday).toString(); 
} 
3

试试这个;

public String toDate() { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     Calendar cal = Calendar.getInstance(); 
     cal.add(Calendar.DATE, -1); 
     return dateFormat.format(cal.getTime()); 
    } 
29

你可以做到以下几点:

private Date getMeYesterday(){ 
    return new Date(System.currentTimeMillis()-24*60*60*1000); 
} 

注意:如果你想每天用24 * 60 * 60 * 1000例如进一步向后日期乘号:

private Date getPreviousWeekDate(){ 
    return new Date(System.currentTimeMillis()-7*24*60*60*1000); 
} 

同样,您可以通过将值添加到System.currentTimeMillis()来获得将来的日期,例如:

private Date getMeTomorrow(){ 
    return new Date(System.currentTimeMillis()+24*60*60*1000); 
} 
+1

我不知道在闰秒的边缘会发生什么...... – codeaperature 2016-11-16 06:38:20

+0

你可以使用TimeUnit.DAYS.toMillis(1)改为计算第 – John 2017-07-11 09:19:48

+0

日,如果NASA使用你的代码去火星会发生什么? – 2018-01-22 08:44:01