大家好,我是雄雄。
前言
大家先看下面的一段话:
今天是:2022年12月18日,星期日,农历十一月廿五,早安🌞🌞🌞
1.讣告 | 我国著名眼科专家兰绪达在南昌逝世,享年89岁
2.北京市新冠病毒疫苗接种情况
3.北京:首都儿研所附属儿童医院防控“新十条”出炉
4.【Nature】为长期新冠提供信息:新冠病毒片段可在79个人体部位中持续存在
5.国家卫健委:昨日新增本土确诊病例2028例 | 全国疫情最新消息(2022.12.18)
6.第九届“树兰医学奖”名单揭晓!
7.【PNAS】肥胖促进血管生成,唤醒休眠肿瘤!最新研究揭示为何肥胖增加癌症风险
8.刘清泉:建议两次抗原阴性就可以返岗
9.《世界疟疾报告2022》发布
10.沈阳市138家二级以上医院全部设置发热门诊
11.重磅!国家卫生健康委通报2022年健康促进医院优秀案例征集结果(附名单))
12.两部门发布最新指引,事关血站新冠病毒感染防控工作
13.韩文秀:我国三年有效应对全球五波疫情冲击
14.《河南省临床专科能力建设实施方案(2022-2025年)》发布!
15.中山医学院,发布讣告!成建定教授去世,终年49岁
详情请点击:https://88688.team/articles/null 查看~
【早安寄语】不要让别人告诉你,你不能做什么。那些做不到的人总要告诉你,你也不行。只要有梦想,就要去追求。
这样的一段话,你是否在别的群中有看到过?尤其是一些做微商,做销售的朋友在群的话,他们每天早上一睁眼的第一件事儿就是将最新新闻简短的发送在群中。
不知道为啥,可能是为了显示自己的存在,亦或者是为了群的活跃吧。
前面我也有写文章,如何实现给群中推送每日新闻,但是有个细节未实现,就是农历和周的问题,今天我们把这个遗憾补上。
实现思路
我们在java中,是可以直接获取到今天周几,还有当前的时间的,但是农历是获取不到的,所以我们需要手写一个工具类,对其进行转换,公历与农历之间的转换,虽然说复杂,但是也是有规律的,网上一找一大堆,我这边就简单的列出来。
代码实现
日期工具类:
public class DateUtils {public static final String STARTTIME = " 00:00:00";public static final String ENDTIME = " 23:59:59";public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";public final static String[] REPLACE_STRING = new String[]{"GMT+0800", "GMT+08:00"};public final static String SPLIT_STRING = "(中国标准时间)";public static Logger log = LoggerFactory.getLogger(DateUtils.class);public static String YYYY = "yyyy";public static String YYYY_MM = "yyyy-MM";public static String YYYY_MM_DD = "yyyy-MM-dd";public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM","yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM","yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};private int year;private int month;private int day;private boolean leap;final static String chineseNumber[] = { "一", "二", "三", "四", "五", "六", "七","八", "九", "十", "十一", "十二" };public static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");final static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570,0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50,0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0,0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550,0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950,0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260,0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0,0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3,0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960,0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0,0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0,0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65,0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0,0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2,0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };// ====== 传回农历 y年的总天数final private static int yearDays(int y) {int i, sum = 348;for (i = 0x8000; i > 0x8; i >>= 1) {if ((lunarInfo[y - 1900] & i) != 0) {sum += 1;}}return (sum + leapDays(y));}// ====== 传回农历 y年闰月的天数final private static int leapDays(int y) {if (leapMonth(y) != 0) {if ((lunarInfo[y - 1900] & 0x10000) != 0) {return 30;} else {return 29;}} else {return 0;}}// ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0final private static int leapMonth(int y) {return (int) (lunarInfo[y - 1900] & 0xf);}// ====== 传回农历 y年m月的总天数final private static int monthDays(int y, int m) {if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0) {return 29;} else {return 30;}}// ====== 传回农历 y年的生肖final public String animalsYear() {final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇","马", "羊", "猴", "鸡", "狗", "猪" };return Animals[(year - 4) % 12];}// ====== 传入 月日的offset 传回干支, 0=甲子final private static String cyclicalm(int num) {final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚","辛", "壬", "癸" };final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午","未", "申", "酉", "戌", "亥" };return (Gan[num % 10] + Zhi[num % 12]);}// ====== 传入 offset 传回干支, 0=甲子final public String cyclical() {int num = year - 1900 + 36;return (cyclicalm(num));}/** *//*** * 传出y年m月d日对应的农历. * yearCyl3:农历年与1864的相差数 ? ** monCyl4:从1900年1月31日以来,闰月数 * dayCyl5:与1900年1月31日相差的天数,再加40 ? * @param* cal * @return */public DateUtils(Calendar cal) {@SuppressWarnings("unused")int yearCyl, monCyl, dayCyl;int leapMonth = 0;Date baseDate = null;try {baseDate = chineseDateFormat.parse("1900年1月31日");} catch (ParseException e) {e.printStackTrace(); // To change body of catch statement use// Options | File Templates.}// 求出和1900年1月31日相差的天数int offset = (int) ((cal.getTime().getTime() - baseDate.getTime()) / 86400000L);dayCyl = offset + 40;monCyl = 14;// 用offset减去每农历年的天数// 计算当天是农历第几天// i最终结果是农历的年份// offset是当年的第几天int iYear, daysOfYear = 0;for (iYear = 1900; iYear < 2050 && offset > 0; iYear++) {daysOfYear = yearDays(iYear);offset -= daysOfYear;monCyl += 12;}if (offset < 0) {offset += daysOfYear;iYear--;monCyl -= 12;}// 农历年份year = iYear;yearCyl = iYear - 1864;leapMonth = leapMonth(iYear); // 闰哪个月,1-12leap = false;// 用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天int iMonth, daysOfMonth = 0;for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {// 闰月if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {--iMonth;leap = true;daysOfMonth = leapDays(year);} else {daysOfMonth = monthDays(year, iMonth);}offset -= daysOfMonth;// 解除闰月if (leap && iMonth == (leapMonth + 1)) {leap = false;}if (!leap) {monCyl++;}}// offset为0时,并且刚才计算的月份是闰月,要校正if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {if (leap) {leap = false;} else {leap = true;--iMonth;--monCyl;}}// offset小于0时,也要校正if (offset < 0) {offset += daysOfMonth;--iMonth;--monCyl;}month = iMonth;day = offset + 1;}public static String getChinaDayString(int day) {String chineseTen[] = { "初", "十", "廿", "卅" };int n = day % 10 == 0 ? 9 : day % 10 - 1;if (day > 30) {return "";}if (day == 10) {return "初十";} else {return chineseTen[day / 10] + chineseNumber[n];}}@Overridepublic String toString() {return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月"+ getChinaDayString(day);}
然后我们在业务层直接调就可以了。
public void sendWechat(JSONObject contentObject) {String url = WEI_MI_SHU_API + "/openapi/v1/chat/room";WeiZhiMiShuVO weiZhiMiShuVO = new WeiZhiMiShuVO();weiZhiMiShuVO.setType(1);//拿到发送的消息列表JSONArray array = contentObject.getJSONArray("list");String todayDate = contentObject.getString("todayDate");StringBuilder sb = new StringBuilder("今天是:" + todayDate);//根据阳历计算农历Calendar today = Calendar.getInstance();// 获得今天是一周的第几天,星期日是第一天,星期二是第二天......// 因为按中国礼拜一作为第一天所以这里减1int dayOfWeek = today.get(Calendar.DAY_OF_WEEK) - 1;String week = "";switch (dayOfWeek) {case 0:week = "日";break;case 1:week = "一";break;case 2:week = "二";break;case 3:week = "三";break;case 4:week = "四";break;case 5:week = "五";break;case 6:week = "六";break;}try {today.setTime(DateUtils.chineseDateFormat.parse(todayDate));DateUtils lunar = new DateUtils(today);String nongli = lunar.toString();nongli = nongli.substring(nongli.indexOf("年")+1);sb.append(",星期").append(week).append(",农历").append(nongli).append(",早安🌞🌞🌞").append("\n");} catch (ParseException e) {e.printStackTrace();}int index = 1;for (Object o : array) {JSONObject item = (JSONObject) o;//标题加时间sb.append("\n").append(index).append(".").append(item.getString("title")).append("\n");;index++;}sb.append("\n详情请点击:https://88688.team/articles/").append(contentObject.getString("aid")).append(" 查看~\n");sb.append("\n【早安寄语】").append(goodMorning());System.out.println("发送的消息为:" + sb.toString());weiZhiMiShuVO.setContent(sb.toString());/*for (String str : SEND_GROUP) {JSONObject object = new JSONObject();object.put("apiKey", WEI_MI_SHU_KEY);object.put("roomName", str);object.put("message", weiZhiMiShuVO);HttpUtil.createPost(url).body(JSON.toJSONString(object)).execute().body();}*/}
大家可以只看关键部分代码即可,运行结果如下图所示: