其他分享
首页 > 其他分享> > 日期时间比较大小(yyyy-MM-dd或者yyyy-MM-dd HH:mm:ss格式比较大小)、获取一年内所有周六日时间和当天之后的周六日时间

日期时间比较大小(yyyy-MM-dd或者yyyy-MM-dd HH:mm:ss格式比较大小)、获取一年内所有周六日时间和当天之后的周六日时间

作者:互联网

package com.common.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtils {
	
	public static String PATTERN10 = "yyyy-MM-dd";
	
	public static String PATTERN19 = "yyyy-MM-dd HH:mm:ss";
	
	public static void main(String[] args) throws Exception {
		String date1 = "2020-11-26 10:01:23";
		String date2 = "2020-11-20 10:01:23";
		SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN10);
		Date dt1 = dateFormat.parse(date1);
    	Date dt2 = dateFormat.parse(date2);
    	System.out.println("date1和date2年月日时分秒比较大小:" + dateCompareTime(date1, date2));
    	System.out.println("date1和date2年月日比较大小:" + dateCompare(date1, date2));
    	System.out.println("dt1和dt2比较大小:" + dateCompare(dt1, dt2));
		System.out.println("当前日之后的周六日时间集:" + weekendDate());
		System.out.println("一年中的所有周六日时间集:" + weekendDate(2020));
		
	}
	
	/**
	 * 获取当前日之后的周六日时间
	 * @title: weekendDate 
	 * @return
	 */
	public static List<String> weekendDate() {
		System.out.println("======获取当前日之后的周六日时间======");
		List<String> dateList = new ArrayList<String>();
		Date date = new Date();
		Calendar cDate = Calendar.getInstance();
		int year = cDate.get(Calendar.YEAR);       // 获取当前年份
		SimpleDateFormat simdf = new SimpleDateFormat(PATTERN10);
		String date1 = simdf.format(date);
		System.out.println(date1);    // 当天日期
		Calendar calendar = new GregorianCalendar(year, 0, 1);
		int i = 1;
		while (calendar.get(Calendar.YEAR) < year + 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, i++);
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			String date2 = simdf.format(calendar.getTime());
			if (calendar.get(Calendar.YEAR) == year) {
				if (!dateCompare(date1, date2)) {
					System.out.println("周日:" + date2);
					dateList.add(simdf.format(calendar.getTime()));
				}
			}
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			String date3 = simdf.format(calendar.getTime());
			if (calendar.get(Calendar.YEAR) == year) {
				if (!dateCompare(date1, date3)) {
					System.out.println("周六:" + date3);
					dateList.add(simdf.format(calendar.getTime()));
				}
			}
		}
		return dateList;
	}
	
	/**
	 * 获取一年中的所有周六日时间
	 * @title: weekendDate 
	 * @param year 年份
	 * @return
	 */
	public static List<String> weekendDate(int year) {
		System.out.println("======获取一年中的所有周六日时间======");
		List<String> dateList = new ArrayList<String>();
		SimpleDateFormat simdf = new SimpleDateFormat(PATTERN10);
		Calendar calendar = new GregorianCalendar(year, 0, 1);
		int i = 1;
		while (calendar.get(Calendar.YEAR) < year + 1) {
			calendar.set(Calendar.WEEK_OF_YEAR, i++);
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			if (calendar.get(Calendar.YEAR) == year) {
				System.out.println("周日:" + simdf.format(calendar.getTime()));
				dateList.add(simdf.format(calendar.getTime()));
			}
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			if (calendar.get(Calendar.YEAR) == year) {
				System.out.println("周六:" + simdf.format(calendar.getTime()));
				dateList.add(simdf.format(calendar.getTime()));
			}
		}
		return dateList;
	}
	

	/**
	 * @title: dateCompare
	 * @description: 比较日期大小:yyyy-MM-dd HH:mm:ss String类型
	 * @param date1 日期1
	 * @param date2 日期2
	 * @return
	 */
	public static boolean dateCompareTime(String date1, String date2) {
		boolean bf = false;
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN19);
			Date dt1 = dateFormat.parse(date1);
			Date dt2 = dateFormat.parse(date2);
			// 比较大小
			if (dt1.getTime() > dt2.getTime()) {
				bf = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bf;
	}
	
	
	/**
     * @title: dateCompare
     * @description: 比较日期大小:yyyy-MM-dd String类型
     * @param date1 日期1
     * @param date2 日期2
     * @return
     */
	public static boolean dateCompare(String date1, String date2) {
		boolean bf = false;
        try {
        	SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN10);
        	Date dt1 = dateFormat.parse(date1);
        	Date dt2 = dateFormat.parse(date2);
        	// 比较大小
        	if (dt1.getTime() > dt2.getTime()) {
        		bf = true;
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
        return bf;
    }
	
	/**
     * @title: dateCompare
     * @description: 比较日期大小:yyyy-MM-dd Date类型
     * @param date1 日期1
     * @param date2 日期2
     * @return
     */
	public static boolean dateCompare(Date date1, Date date2) {
		boolean bf = false;
		// 比较大小
    	if (date1.getTime() > date2.getTime()) {
    		bf = true;
        }
        return bf;
    }

}

标签:date1,date2,String,MM,dd,yyyy,getTime,calendar,Calendar
来源: https://blog.csdn.net/u013526643/article/details/110198757