java get 的快捷方法(Java get yesterday’s date)
Java get yesterdays date
Posted on: November 1, 2008 at 12:00 AM
This section illustrates you how to obtain the yesterdays
Java get yesterdays dateThis section illustrates you how to obtain the yesterdays date.
In the given example, we simply get the current date by using the method dateFormat.format(cal.getTime()).But on subtracting one day from the calendar by using the method cal.add(Calendar.DATE, -1), the method dateFormat.format(cal.getTime()) will displayed the yesterdays date.
cal.getTime()-This method returns the Date object representing the time value of Calendar.
Here is the code of GetYesterdayDate.java
import java.text.*;
import java.sql.Date;
import java.util.Calendar;public class GetYesterdayDate{
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("Todays date is "+dateFormat.format(cal.getTime()));cal.add(Calendar.DATE, -1);
System.out.println("Yesterdays date was "+dateFormat.format(cal.getTime()));
}
}创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!