0.0.3:
添加了计算农历生日的方法
This commit is contained in:
@@ -12,7 +12,7 @@ public class PushTask {
|
||||
PushDailyController pushDailyController;
|
||||
|
||||
//每日 早上8,12,22点 定时推送
|
||||
@Scheduled(cron = "0 0 8,12,22 * * ?")
|
||||
@Scheduled(cron = "0 0 8 * * ?")
|
||||
public void scheduledPush(){
|
||||
pushDailyController.push();
|
||||
}
|
||||
|
||||
@@ -1,15 +1,97 @@
|
||||
package com.bamanker.dailylove.utils;
|
||||
|
||||
import cn.hutool.core.date.ChineseDate;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.bamanker.dailylove.config.DailyLoveConfigure;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.util.Strings;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@Slf4j
|
||||
public class DataUtils {
|
||||
|
||||
/**
|
||||
* 计算农历生日天数
|
||||
*
|
||||
* @param birthday
|
||||
* @return
|
||||
*/
|
||||
public static int getChineseBirthdays(String birthday) {
|
||||
//获取输入的生日
|
||||
String boyBirthday = DailyLoveConfigure.Boy_Birthday;
|
||||
//获取农历生日
|
||||
ChineseDate chineseBir = new ChineseDate(DateUtil.parseDate(DailyLoveConfigure.Boy_Birthday));
|
||||
log.info("生日的农历日期是:{}", chineseBir);
|
||||
//截取日、月
|
||||
String[] strings = Strings.split(boyBirthday, '-');
|
||||
int chineseMonth = chineseBir.getMonth();
|
||||
int chineseDay = chineseBir.getDay();
|
||||
//获取当前日期的年
|
||||
Calendar dateToday = Calendar.getInstance();
|
||||
int todayYear = dateToday.get(Calendar.YEAR);
|
||||
//把生日的年改为今年,方便计算
|
||||
ChineseDate chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay);
|
||||
//农历日期对应的阳历日期
|
||||
int gregorianDay = chineseDate.getGregorianDay();
|
||||
//计算时间差
|
||||
long days = haveThisDay(chineseMonth, chineseDay, dateToday, todayYear, chineseDate, gregorianDay);
|
||||
log.info("days:{}", days);
|
||||
return (int) days;
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要递归计算日期差
|
||||
*
|
||||
* @param chineseMonth 农历月
|
||||
* @param chineseDay 农历日
|
||||
* @param dateToday 今天的日期类
|
||||
* @param todayYear 当前的年
|
||||
* @param chineseDate 组装的待计算的新日期
|
||||
* @param gregorianDay 判断是否存在农历日期的参数,-1代表今年不存在这个农历日期
|
||||
* @return 计算好的天数
|
||||
*/
|
||||
private static long haveThisDay(int chineseMonth, int chineseDay, Calendar dateToday, int todayYear, ChineseDate chineseDate, int gregorianDay) {
|
||||
//判断当前年份是否存在农历日对应的阳历日
|
||||
while (gregorianDay == -1) {
|
||||
//不存在,计算明年
|
||||
todayYear += 1;
|
||||
chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay, false);
|
||||
gregorianDay = chineseDate.getGregorianDay();
|
||||
}
|
||||
//存在
|
||||
//将农历日期改为阳历日期
|
||||
Calendar gregorianbir = chineseDate.getGregorianCalendar();
|
||||
//判断这个日期是否和今年是同一年
|
||||
if (todayYear == dateToday.get(Calendar.YEAR)) {
|
||||
//是同一年,判断这一天过了没
|
||||
if (gregorianbir.get(Calendar.DAY_OF_YEAR) < dateToday.get(Calendar.DAY_OF_YEAR)) {
|
||||
//这一天过了,计算明年
|
||||
todayYear += 1;
|
||||
chineseDate = new ChineseDate(todayYear, chineseMonth, chineseDay, false);
|
||||
gregorianDay = chineseDate.getGregorianDay();
|
||||
//递归计算下一年是否存在对应的阳历日
|
||||
return haveThisDay(chineseMonth, chineseDay, dateToday, todayYear, chineseDate, gregorianDay);
|
||||
}
|
||||
//没有过,直接计算
|
||||
}
|
||||
//不是同一年,直接计算
|
||||
Date time = dateToday.getTime();
|
||||
log.info("当前的日期是:{}", time);
|
||||
Date time1 = gregorianbir.getTime();
|
||||
log.info("下一个生日的日期:{}", time1);
|
||||
return DateUtil.between(dateToday.getTime(), gregorianbir.getTime(), DateUnit.DAY);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算生日天数 days
|
||||
*
|
||||
* @param birthday
|
||||
* @return
|
||||
*/
|
||||
@@ -42,6 +124,7 @@ public class DataUtils {
|
||||
|
||||
/**
|
||||
* 计算恋爱天数 days
|
||||
*
|
||||
* @param loveday
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -23,7 +23,7 @@ DL:
|
||||
tianxin-key: 72fbbb9e75e338ea6a240e83972f287c
|
||||
city-id: 101270101
|
||||
girl-birthday: 1995-06-28
|
||||
boy-birthday: 1995-03-30
|
||||
boy-birthday: 1995-3-30
|
||||
love-day: 2022-07-16
|
||||
|
||||
wechat:
|
||||
|
||||
Reference in New Issue
Block a user