- package test.user.taojq;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Collections;
- import java.util.Date;
- public class Test {
- public static void main(String[] argStrings){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String star=sdf.format(new Date());
- String end=sdf.format(new Date(32323232L));
- compareDate(star, end, 0);
- }
- /**
- * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
- * @param date2 被比较的时间 为空(null)则为当前时间
- * @param stype 返回值类型 0为多少天,1为多少个月,2为多少年
- * @return
- * 举例:
- * compareDate("2009-09-12", null, 0);//比较天
- * compareDate("2009-09-12", null, 1);//比较月
- * compareDate("2009-09-12", null, 2);//比较年
- */
- public static int compareDate(String startDay,String endDay,int stype){
- int n = 0;
- String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";
- DateFormat df = new SimpleDateFormat(formatStyle);
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
- try {
- c1.setTime(df.parse(startDay));
- c2.setTime(df.parse(endDay));
- } catch (Exception e3) {
- System.out.println("wrong occured");
- }
- while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
- n++;
- if(stype==1){
- c1.add(Calendar.MONTH, 1); // 比较月份,月份+1
- }
- else{
- c1.add(Calendar.DATE, 1); // 比较天数,日期+1
- }
- }
- n = n-1;
- if(stype==2){
- n = (int)n/365;
- }
- return n;
- }
- public static String getCurrentDate(String format){
- Calendar day=Calendar.getInstance();
- day.add(Calendar.DATE,0);
- SimpleDateFormat sdf=new SimpleDateFormat(format);//"yyyy-MM-dd"
- String date = sdf.format(day.getTime());
- return date;
- }
- }
日期比较
最新推荐文章于 2023-09-14 19:17:44 发布