public class Demo_date2 {
public static void main(String[] args) throws ParseException {
System.out.println("请输入你要判断的年份");
//输入年份
int year = new Scanner(System.in).nextInt();
Calendar cal = Calendar.getInstance();
//日历插入年月日
cal.set(year, 12, 31);
//System.out.println(cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH));
//把字符串时间转化为date形式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String old = cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH);
System.out.println(old);
Date old_date = sdf.parse(old);
SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd D");
System.out.println(sdff.format(old_date));
//System.out.println(old_date);
//打印时间的具体数字毫秒
long lod_time = old_date.getTime();
//System.out.println(lod_time);
System.out.println("================");
//System.out.println(cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH));
//让日历时间-1
cal.add(Calendar.YEAR, -1);
System.out.println(cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH));
String now = cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.DAY_OF_MONTH);
System.out.println(now);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date old_date_1 = sdf.parse(now);
long lod_time_1 = old_date_1.getTime();
System.out.println(lod_time_1);
long last = lod_time-lod_time_1;
System.out.println(last);
System.out.println(year+"这一年一共"+last/1000/60/60/24+"天");
}
} 作者: Zzh94520 时间: 2016-9-8 23:21
什么叫优化