0.0.3:
添加了计算农历生日的方法
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>com.bamanker</groupId>
|
||||
<artifactId>dailyLove</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<version>0.0.3</version>
|
||||
<name>dailyLove</name>
|
||||
<description>dailyLove</description>
|
||||
<properties>
|
||||
@@ -20,8 +20,14 @@
|
||||
<fastjson.version>2.0.21</fastjson.version>
|
||||
<openfeign.version>3.1.5</openfeign.version>
|
||||
<nacos.version>2021.1</nacos.version>
|
||||
<hutool.version>5.8.10</hutool.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>${hutool.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
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.JSONObject;
|
||||
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.utils.DataUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.util.Strings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@SpringBootTest
|
||||
@Slf4j
|
||||
@@ -27,12 +36,100 @@ class DailyLoveApplicationTests {
|
||||
|
||||
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
|
||||
void test1() {
|
||||
int loveDays = getLoveDays("2021-07-16");
|
||||
log.info("-------------------------------{}", loveDays);
|
||||
}
|
||||
|
||||
public static int getLoveDays(String loveday) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
int days = 0;
|
||||
@@ -45,6 +142,7 @@ class DailyLoveApplicationTests {
|
||||
}
|
||||
return days;
|
||||
}
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
TianXinReqParam param1 = new TianXinReqParam();
|
||||
|
||||
Reference in New Issue
Block a user