0.0.3:
添加了计算农历生日的方法
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -10,7 +10,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
<groupId>com.bamanker</groupId>
|
<groupId>com.bamanker</groupId>
|
||||||
<artifactId>dailyLove</artifactId>
|
<artifactId>dailyLove</artifactId>
|
||||||
<version>0.0.2</version>
|
<version>0.0.3</version>
|
||||||
<name>dailyLove</name>
|
<name>dailyLove</name>
|
||||||
<description>dailyLove</description>
|
<description>dailyLove</description>
|
||||||
<properties>
|
<properties>
|
||||||
@@ -20,8 +20,14 @@
|
|||||||
<fastjson.version>2.0.21</fastjson.version>
|
<fastjson.version>2.0.21</fastjson.version>
|
||||||
<openfeign.version>3.1.5</openfeign.version>
|
<openfeign.version>3.1.5</openfeign.version>
|
||||||
<nacos.version>2021.1</nacos.version>
|
<nacos.version>2021.1</nacos.version>
|
||||||
|
<hutool.version>5.8.10</hutool.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-core</artifactId>
|
||||||
|
<version>${hutool.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class PushTask {
|
|||||||
PushDailyController pushDailyController;
|
PushDailyController pushDailyController;
|
||||||
|
|
||||||
//每日 早上8,12,22点 定时推送
|
//每日 早上8,12,22点 定时推送
|
||||||
@Scheduled(cron = "0 0 8,12,22 * * ?")
|
@Scheduled(cron = "0 0 8 * * ?")
|
||||||
public void scheduledPush(){
|
public void scheduledPush(){
|
||||||
pushDailyController.push();
|
pushDailyController.push();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,97 @@
|
|||||||
package com.bamanker.dailylove.utils;
|
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 org.springframework.util.unit.DataUnit;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class DataUtils {
|
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
|
* 计算生日天数 days
|
||||||
|
*
|
||||||
* @param birthday
|
* @param birthday
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -42,6 +124,7 @@ public class DataUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算恋爱天数 days
|
* 计算恋爱天数 days
|
||||||
|
*
|
||||||
* @param loveday
|
* @param loveday
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ DL:
|
|||||||
tianxin-key: 72fbbb9e75e338ea6a240e83972f287c
|
tianxin-key: 72fbbb9e75e338ea6a240e83972f287c
|
||||||
city-id: 101270101
|
city-id: 101270101
|
||||||
girl-birthday: 1995-06-28
|
girl-birthday: 1995-06-28
|
||||||
boy-birthday: 1995-03-30
|
boy-birthday: 1995-3-30
|
||||||
love-day: 2022-07-16
|
love-day: 2022-07-16
|
||||||
|
|
||||||
wechat:
|
wechat:
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package com.bamanker.dailylove;
|
package com.bamanker.dailylove;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.ChineseDate;
|
||||||
|
import cn.hutool.core.date.DateTime;
|
||||||
|
import cn.hutool.core.date.DateUnit;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.bamanker.dailylove.config.DailyLoveConfigure;
|
import com.bamanker.dailylove.config.DailyLoveConfigure;
|
||||||
@@ -10,13 +14,18 @@ import com.bamanker.dailylove.domain.Weather;
|
|||||||
import com.bamanker.dailylove.service.DataRemoteClient;
|
import com.bamanker.dailylove.service.DataRemoteClient;
|
||||||
import com.bamanker.dailylove.utils.DataUtils;
|
import com.bamanker.dailylove.utils.DataUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.bouncycastle.util.Strings;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.util.unit.DataUnit;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -27,12 +36,100 @@ class DailyLoveApplicationTests {
|
|||||||
|
|
||||||
String remark = "❤";
|
String remark = "❤";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test3() {
|
||||||
|
|
||||||
|
|
||||||
|
//获取输入的生日
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要递归计算日期差
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test2() {
|
||||||
|
|
||||||
|
TianXinReqParam param1 = new TianXinReqParam();
|
||||||
|
param1.setKey(DailyLoveConfigure.TianXin_Key);
|
||||||
|
param1.setCity(DailyLoveConfigure.City_ID);
|
||||||
|
param1.setType("7");
|
||||||
|
String weatherJson = dataRemoteClient.getWeather(param1);
|
||||||
|
log.info("weather:{}", weatherJson);
|
||||||
|
JSONObject resWeather = JSONObject.parseObject(weatherJson);
|
||||||
|
JSONObject result = resWeather.getJSONArray("result").getJSONObject(0).getJSONArray("list").getJSONObject(1);
|
||||||
|
log.info("----result----:{}", result);
|
||||||
|
Weather weather = result.toJavaObject(Weather.class);
|
||||||
|
log.info("weather:{}", weather);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test1() {
|
void test1() {
|
||||||
int loveDays = getLoveDays("2021-07-16");
|
int loveDays = getLoveDays("2021-07-16");
|
||||||
log.info("-------------------------------{}",loveDays);
|
log.info("-------------------------------{}", loveDays);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getLoveDays(String loveday) {
|
public static int getLoveDays(String loveday) {
|
||||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
int days = 0;
|
int days = 0;
|
||||||
@@ -45,6 +142,7 @@ class DailyLoveApplicationTests {
|
|||||||
}
|
}
|
||||||
return days;
|
return days;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void contextLoads() {
|
||||||
TianXinReqParam param1 = new TianXinReqParam();
|
TianXinReqParam param1 = new TianXinReqParam();
|
||||||
@@ -66,12 +164,12 @@ class DailyLoveApplicationTests {
|
|||||||
ResultVo resultVo = ResultVo.initializeResultVo(DailyLoveConfigure.Open_ID,
|
ResultVo resultVo = ResultVo.initializeResultVo(DailyLoveConfigure.Open_ID,
|
||||||
DailyLoveConfigure.Template_ID,
|
DailyLoveConfigure.Template_ID,
|
||||||
DailyLoveConfigure.Color_Top);
|
DailyLoveConfigure.Color_Top);
|
||||||
resultVo.setAttribute("now",new DataItem(weather.getDate() + " "+weather.getWeek(),"#00BFFF"));
|
resultVo.setAttribute("now", new DataItem(weather.getDate() + " " + weather.getWeek(), "#00BFFF"));
|
||||||
resultVo.setAttribute("city",new DataItem(weather.getArea(),null));
|
resultVo.setAttribute("city", new DataItem(weather.getArea(), null));
|
||||||
resultVo.setAttribute("weather",new DataItem(weather.getWeather(),"#1f95c5"));
|
resultVo.setAttribute("weather", new DataItem(weather.getWeather(), "#1f95c5"));
|
||||||
resultVo.setAttribute("min_temperature",new DataItem(weather.getLowest(),"#0ace3c"));
|
resultVo.setAttribute("min_temperature", new DataItem(weather.getLowest(), "#0ace3c"));
|
||||||
resultVo.setAttribute("max_temperature",new DataItem(weather.getHighest(),"#dc1010"));
|
resultVo.setAttribute("max_temperature", new DataItem(weather.getHighest(), "#dc1010"));
|
||||||
resultVo.setAttribute("tips",new DataItem(weather.getTips(),null));
|
resultVo.setAttribute("tips", new DataItem(weather.getTips(), null));
|
||||||
|
|
||||||
|
|
||||||
TianXinReqParam param2 = new TianXinReqParam();
|
TianXinReqParam param2 = new TianXinReqParam();
|
||||||
@@ -81,8 +179,8 @@ class DailyLoveApplicationTests {
|
|||||||
JSONObject responseRainbow = JSONObject.parseObject(rainbowJson);
|
JSONObject responseRainbow = JSONObject.parseObject(rainbowJson);
|
||||||
String rainbow = responseRainbow.getJSONArray("result").getJSONObject(0).getString("content");
|
String rainbow = responseRainbow.getJSONArray("result").getJSONObject(0).getString("content");
|
||||||
log.info("rainbow:{}", rainbow);
|
log.info("rainbow:{}", rainbow);
|
||||||
resultVo.setAttribute("daily_english_cn",new DataItem(rainbow,"#FF69B4"));
|
resultVo.setAttribute("daily_english_cn", new DataItem(rainbow, "#FF69B4"));
|
||||||
resultVo.setAttribute("remark",new DataItem(remark,"#FF1493"));
|
resultVo.setAttribute("remark", new DataItem(remark, "#FF1493"));
|
||||||
|
|
||||||
|
|
||||||
String englishJson = dataRemoteClient.getDailyEnglish(param2);
|
String englishJson = dataRemoteClient.getDailyEnglish(param2);
|
||||||
@@ -90,15 +188,15 @@ class DailyLoveApplicationTests {
|
|||||||
JSONObject responseEnglist = JSONObject.parseObject(englishJson);
|
JSONObject responseEnglist = JSONObject.parseObject(englishJson);
|
||||||
String englist = responseEnglist.getJSONArray("result").getJSONObject(0).getString("en");
|
String englist = responseEnglist.getJSONArray("result").getJSONObject(0).getString("en");
|
||||||
log.info("english:{}", englist);
|
log.info("english:{}", englist);
|
||||||
resultVo.setAttribute("daily_english_en",new DataItem(rainbow,"#800080"));
|
resultVo.setAttribute("daily_english_en", new DataItem(rainbow, "#800080"));
|
||||||
|
|
||||||
int birthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday);
|
int birthdays = DataUtils.getBirthdays(DailyLoveConfigure.Girl_Birthday);
|
||||||
log.info("bir_day:{}", birthdays);
|
log.info("bir_day:{}", birthdays);
|
||||||
resultVo.setAttribute("bir_day",new DataItem(birthdays+"","#FFA500"));
|
resultVo.setAttribute("bir_day", new DataItem(birthdays + "", "#FFA500"));
|
||||||
|
|
||||||
int loveDays = DataUtils.getLoveDays(DailyLoveConfigure.Love_Day);
|
int loveDays = DataUtils.getLoveDays(DailyLoveConfigure.Love_Day);
|
||||||
log.info("love_day:{}", loveDays);
|
log.info("love_day:{}", loveDays);
|
||||||
resultVo.setAttribute("love_day",new DataItem(loveDays+"","#FFA500"));
|
resultVo.setAttribute("love_day", new DataItem(loveDays + "", "#FFA500"));
|
||||||
|
|
||||||
log.info("resultVo:{}", resultVo);
|
log.info("resultVo:{}", resultVo);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user