直接上传代码:
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class CalendarDemo {
public static void main(String[] args) throws ParseException {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println("今天是:"+year+"年"+month+"月"+day+"日"+" "
+hour+":"+minute+":"+second);
String hDay = "2017/09/10 12:00:00";
String qDay = "2016/08/17 13:00:14";
long days = calcDays(hDay, qDay);
System.out.println(days);
}
public static long calcDays(String date1,String date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/ss HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date1));
long time1 = c.getTimeInMillis();
c.setTime(sdf.parse(date2));
long time2 = c.getTimeInMillis();
return (time1-time2)/(24*3600*1000);
}
}
热门评论
不同月份的天数问题,系统已经帮助解决了
用Period几行代码搞定