当前位置: 技术问答>java相关
Java中怎样取得前一天和下一天的日期?
来源: 互联网 发布时间:2017-05-01
本文导语: 有什么简便的方法可以取得前一天和下一天的日期,不用自己写这一段算法。 | Calendar.set(Calendar.DAY_OF_YEAR, +1) Calendar.set(Calendar.DAY_OF_YEAR, -1) | 同意 | 用滚动 ...
有什么简便的方法可以取得前一天和下一天的日期,不用自己写这一段算法。
|
Calendar.set(Calendar.DAY_OF_YEAR, +1)
Calendar.set(Calendar.DAY_OF_YEAR, -1)
Calendar.set(Calendar.DAY_OF_YEAR, -1)
|
同意
|
用滚动
Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR,1);//明天
now.roll(Calendar.DAY_OF_YEAR,-1);//昨天
Calendar now = Calendar.getInstance();
now.roll(Calendar.DAY_OF_YEAR,1);//明天
now.roll(Calendar.DAY_OF_YEAR,-1);//昨天
|
up
|
java.text.SimpleDateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Calendar calendar=java.util.Calendar.getInstance();
calendar.roll(java.util.Calendar.DAY_OF_YEAR,-1);
System.out.println("昨天:"+df.format(calendar.getTime()));
calendar=java.util.Calendar.getInstance();
System.out.println("今天:"+df.format(calendar.getTime()));
calendar.roll(java.util.Calendar.DAY_OF_YEAR,1);
System.out.println("明天:"+df.format(calendar.getTime()));
java.util.Calendar calendar=java.util.Calendar.getInstance();
calendar.roll(java.util.Calendar.DAY_OF_YEAR,-1);
System.out.println("昨天:"+df.format(calendar.getTime()));
calendar=java.util.Calendar.getInstance();
System.out.println("今天:"+df.format(calendar.getTime()));
calendar.roll(java.util.Calendar.DAY_OF_YEAR,1);
System.out.println("明天:"+df.format(calendar.getTime()));