一、得到当前时间的前一天
package com.qiyuan.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class getTime { public static String getYesterday(){ Date dNow = new Date(); //当前时间 Date dBefore = new Date(); Calendar calendar = Calendar.getInstance(); //得到日历 calendar.setTime(dNow);//把当前时间赋给日历 calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天 dBefore = calendar.getTime(); //得到前一天的时间 String yesterdayTime = new SimpleDateFormat("yyyy年MM月dd日").format(dBefore); return yesterdayTime; } public static void main(String[] args) { System.out.println(getTime.getYesterday()); } }
核心代码:calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天