ChineseCalendarUtils中国日期

package com.thinkgem.jeesite.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkgem.jeesite.common.Exception.ErpException;

public class ChineseCalendarUtils {

	private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);

	// 法律规定的放假日期
	private static List<String> lawHolidays = Arrays.asList("2018-06-30","2018-07-01","2018-07-07",
			"2018-07-08","2018-07-14","2018-07-15","2018-07-21","2018-07-22","2018-07-28",
			"2018-07-29","2018-08-04","2018-08-05","2018-08-11","2018-08-12","2018-08-18",
			"2018-08-19","2018-08-25","2018-08-26","2018-09-01","2018-09-02","2018-09-08",
			"2018-09-09","2018-09-15","2018-09-16","2018-09-22","2018-09-23","2018-09-24",
			"2018-10-01","2018-10-02","2018-10-03","2018-10-04","2018-10-05","2018-10-06",
			"2018-10-07","2018-10-13","2018-10-14","2018-10-20","2018-10-21","2018-10-27",
			"2018-10-28","2018-11-03","2018-11-04","2018-11-10","2018-11-11","2018-11-17",
			"2018-11-18","2018-11-24","2018-11-25","2018-12-01","2018-12-02","2018-12-08",
			"2018-12-09","2018-12-15","2018-12-16","2018-12-22","2018-12-23","2018-12-29",
			"2018-12-30","2019-01-05","2018-01-06","2018-01-12","2018-01-13","2018-01-19",
			"2018-01-20","2018-01-26","2018-01-27");

	/**
	 * 判断传入时间 是否是 工作日
	 */
	public static boolean isWorkDay(Date day) {
		String date = formattedDateToString(day);
		if(date == null) {
			return false;
		}

		if(lawHolidays.contains(date)) {
			return false;
		}

		return true;
	}

	/**
	 * 获取传入时间 的下一个工作日时间
	 */
	public static String getNextWorkDate(Date day) {
		String date = formattedDateToString(day);

		if(date == null) {
			return null;
		}

		//判断当前时间增加一天,是否在节假日当中
		Date nowdate = formattedDateToDate(date);
		Date addDate = nowdate;
		//是否为工作日
		boolean flag = true;
		do{
			// 时间加一天
			addDate = addDate(addDate, 1);
			String addDateStr = formattedDateToString(addDate);
			// 若日期 在 非工作日集合里,则当前日期不是 工作日
			if (lawHolidays.contains(addDateStr)) {
				flag = false;
			}else {
				flag = true;
			}
		}while(!flag);

		return formattedDateToString(addDate);
	}

	/**
	 * 获取传入时间  距离下一个工作日天数,如 2018-06-29(周五),距离下一个工作日2018-07-02天数3
	 */
	public static int getNextWorkDays(Date day) {
		String date = formattedDateToString(day);

		if(date == null) {
			return 0;
		}

		//判断当前时间增加一天,是否在节假日当中
		Date nowdate = formattedDateToDate(date);
		Date addDate = nowdate;
		//是否为工作日
		boolean flag = true;
		do{
			// 时间加一天
			addDate = addDate(addDate, 1);
			String addDateStr = formattedDateToString(addDate);
			// 若日期 在 非工作日集合里,则当前日期不是 工作日
			if (lawHolidays.contains(addDateStr)) {
				flag = false;
			}else {
				flag = true;
			}
		}while(!flag);

		return (int) ((addDate.getTime()-nowdate.getTime())/(24*60*60*1000));
	}

	/**
	 * 比较时间 date 是否 在 day 之前
	 * @param date
	 * @param time HH:mm:ss
	 * @return date 在 day 之前 返回 true,超过 time 时间 返回 false
	 * @throws ErpException
	 * @throws ParseException
	 */
	public static boolean compareHourMinute(Date date,String time) throws ErpException, ParseException {
		String reg = "[0-9]{2}:[0-9]{2}:[0-9]{2}";//HH:mm:ss
		boolean flag = Pattern.compile(reg).matcher(time).matches();
		if(!flag) {
			throw new ErpException("比较时间格式异常");
		}

		String dateStr = formattedDateToString(date);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date compareDate = formatter.parse(dateStr + " "+time);

		if(date.getTime() >= compareDate.getTime()) {
			return false;
		}
		return true;
	}

	/**
	 * 比较日期相差天数(只比较日期),如 "2018-01-01 23:00:00" 与 "2018-01-02 06:00:00" 相差 1天
	 * @param maxDate
	 * @param minDate
	 * @return
	 */
	public static int getDifferenceDays(Date maxDate, Date minDate) {
		String maxDateStr = formattedDateToString(maxDate);
		String minDateStr = formattedDateToString(minDate);

		maxDate = formattedDateToDate(maxDateStr);
		minDate = formattedDateToDate(minDateStr);

		return (int) ((maxDate.getTime()-minDate.getTime())/(24*60*60*1000));
	}

	/**
	 * 增加天数
	 * @param date
	 * @param days 整数往后推,负数往前移动
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date addDate(Date date,int days) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE,days);//把日期往后增加一天.整数往后推,负数往前移动
		date=calendar.getTime();   //这个时间就是日期往后推一天的结果
		return date;
	}

	/**
	 * 判断是否是法定假日
	 *
	 * @param calendar
	 * @return
	 * @throws Exception
	 */
	public static boolean isLawHoliday(String calendar) throws Exception {
		isMatchDateFormat(calendar);
		if (lawHolidays.contains(calendar)) {
			return true;
		}
		return false;
	}


	/**
	 * 格式化时间 转字符串
	 * @param day
	 * @return yyyy-MM-dd
	 */
	private static String formattedDateToString(Date day) {
		if(day == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String formatStr =formatter.format(day);
		logger.info("格式化时间--"+formatStr);
		return formatStr;
	}

	/**
	 * 格式化字符串 转 时间
	 * @param day "yyyy-MM-dd"
	 * @return
	 */
	private static Date formattedDateToDate(String date) {
		if(date == null) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date day = null;
		try {
			day = formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		logger.info("格式化时间--"+day.toString());
		return day;
	}


	/**
	 * 判断是否是周末
	 *
	 * @param calendar
	 * @return
	 * @throws ParseException
	 */
	public static boolean isWeekends(String calendar) throws Exception {
		isMatchDateFormat(calendar);
		// 先将字符串类型的日期转换为Calendar类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(calendar);
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		if (ca.get(Calendar.DAY_OF_WEEK) == 1
				|| ca.get(Calendar.DAY_OF_WEEK) == 7) {
			return true;
		}
		return false;
	}

	/**
	 * 使用正则表达式匹配日期格式
	 *
	 * @throws Exception
	 */
	private static void isMatchDateFormat(String calendar) throws Exception {
		Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
		Matcher matcher = pattern.matcher(calendar);
		boolean flag = matcher.matches();
		if (!flag) {
			throw new Exception("输入日期格式不正确,应该为2017-12-19");
		}
	}


}

 

# 農曆 Class **農曆** class is in the *org.magiclen.農曆* package. ### Initialize You can't create a **農曆** instance by using **new** operator directly. You have to use the **建立** static method provided by **農曆** class to create a **農曆** instance. You can pass the year, month and the days of the month in Gregorian calendar as parameters to **建立** method. For example, we want to create a **農曆** object instance with the date 2015-12-16 in Gregorian calendar. The code will be, 農曆.建立(2015, 12, 12); For example, we want to create a **農曆** object instance with the date 2015-12-16 in Chinese calendar and we know that date is not in a leap month. The code will be, 農曆 date = 農曆.建立(2015, 12, false, 12); ### Get the date in Gregorian calendar Use **取得西曆** method to get the date in Gregorian calendar. String gregorianDate = date.取得西曆(); ### Get the date in Chinese calendar Use **取得農曆** method to get the date in Chinese calendar. String chineseDate = date.取得農曆(); ### Compute the weight of Ba Zi(八字) Use **計算八字有幾兩重** method to compute the weight of Ba Zi. Because **農曆** object has no hour information, if you want to compute the weight of Ba Zi, you have to provide the hour information in the form of the twelve Earthly Branches. float BaZiWeight = date.計算八字有幾兩重(農曆.地支.子); # Run **Java Chinese Calendar** as a program You can also use the two commands below to run **Java Chinese Calendar**: java -jar JavaChineseCalendar.jar It will show today's date in Gregorian calendar and Chinese calendar. The result just likes 西曆:2015-12-16 農曆:2015(乙未、羊年) 冬月 初六 Yo
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值