import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class NextDay {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入日期,格式为yyyy-MM-dd:");
try {
SimpleDateFormat sformat = new SimpleDateFormat("瞎腊友yyyy-MM-dd");
//取控制台输入
String sDate = reader.readLine();
//解析日期
Date date = sformat.parse(sDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
//用Calendar工具类给日期加一天
c.add(Calendar.DATE, 1);
System.out.println("下一天为:" + sformat.format(c.getTime()));
} catch (IOException e) {
e.printStackTrace();
System.err.println("输入日期异常局迟");
} catch (ParseException e) {
e.printStackTrace();
System.err.println("不磨槐能解析的日期格式");
}
}
}
使用Calendar这个类,在java.util包里,有add的方法,可以添加一天
SimpledateFromate.("xxxxxxxx").toparse("xxxx-xx-xx");
把输入的格式判陵转掘神戚换为日瞎坦期格式
Date date=new Date(xx+1);
就行了
public static void getNextDay(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
long time = (sdf.parse(sdf.format(date))).getTime();
long nextDayTime = time + (1000 * 60 * 60 * 24);
Date d = new Date(nextDayTime);
System.out.println(sdf.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
}