Commit 3fe033e8 by tyfanchz

增加数据生成类

1 parent d4b664fa
Showing 17 changed files with 361 additions and 270 deletions
package cn.com.glsx.openplatform.model;
/**
* <p>Description:
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
......@@ -13,6 +13,11 @@ package cn.com.glsx.openplatform.model;
*/
public class CustomerActionStatistics {
/**
* 在线设备数
*/
private Integer onlineNumber;
/**
* id
*/
private Integer id;
......@@ -28,6 +33,11 @@ public class CustomerActionStatistics {
private String customerName;
/**
* 设备类型
*/
private String deviceType;
/**
* 行为类型
*/
private String type;
......@@ -38,14 +48,17 @@ public class CustomerActionStatistics {
private String statisticsTime;
/**
* 活跃数
* 统计数
*/
private Integer activeNumber;
private Integer number;
/**
* 激活增长数
*/
private Integer activationGrowth;
public Integer getOnlineNumber() {
return onlineNumber;
}
public void setOnlineNumber(Integer onlineNumber) {
this.onlineNumber = onlineNumber;
}
public Integer getId() {
return id;
......@@ -71,6 +84,14 @@ public class CustomerActionStatistics {
this.customerName = customerName;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getType() {
return type;
}
......@@ -87,36 +108,25 @@ public class CustomerActionStatistics {
this.statisticsTime = statisticsTime;
}
public Integer getActiveNumber() {
return activeNumber;
public Integer getNumber() {
return number;
}
public void setActiveNumber(Integer activeNumber) {
this.activeNumber = activeNumber;
}
public Integer getActivationGrowth() {
return activationGrowth;
}
public void setActivationGrowth(Integer activationGrowth) {
this.activationGrowth = activationGrowth;
public void setNumber(Integer number) {
this.number = number;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", customerId=").append(customerId);
sb.append(", customerName=").append(customerName);
sb.append(", type=").append(type);
sb.append(", statisticsTime=").append(statisticsTime);
sb.append(", activeNumber=").append(activeNumber);
sb.append(", activationGrowth=").append(activationGrowth);
sb.append("]");
return sb.toString();
}
}
\ No newline at end of file
return "CustomerActionStatistics{" +
"onlineNumber=" + onlineNumber +
", id=" + id +
", customerId=" + customerId +
", customerName='" + customerName + '\'' +
", deviceType='" + deviceType + '\'' +
", type='" + type + '\'' +
", statisticsTime='" + statisticsTime + '\'' +
", number=" + number +
'}';
}
}
package cn.com.glsx.openplatform.model;
/**
* <p>Description:
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
......@@ -28,24 +28,24 @@ public class CustomerNetworkStatistics {
private String customerName;
/**
* 类型 1月 2年
*/
private Byte type;
* 设备类型
*/
private String deviceType;
/**
* 统计时间
* 类型 月 年
*/
private String statisticsTime;
private String type;
/**
* 活跃数
* 统计时间
*/
private Integer activeNumber;
private String statisticsTime;
/**
* 激活增长数
* 联网设备数
*/
private Integer activationGrowth;
private Integer onlineNumber;
public Integer getId() {
return id;
......@@ -71,11 +71,19 @@ public class CustomerNetworkStatistics {
this.customerName = customerName;
}
public Byte getType() {
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getType() {
return type;
}
public void setType(Byte type) {
public void setType(String type) {
this.type = type;
}
......@@ -87,36 +95,24 @@ public class CustomerNetworkStatistics {
this.statisticsTime = statisticsTime;
}
public Integer getActiveNumber() {
return activeNumber;
}
public void setActiveNumber(Integer activeNumber) {
this.activeNumber = activeNumber;
}
public Integer getActivationGrowth() {
return activationGrowth;
public Integer getOnlineNumber() {
return onlineNumber;
}
public void setActivationGrowth(Integer activationGrowth) {
this.activationGrowth = activationGrowth;
public void setOnlineNumber(Integer onlineNumber) {
this.onlineNumber = onlineNumber;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", customerId=").append(customerId);
sb.append(", customerName=").append(customerName);
sb.append(", type=").append(type);
sb.append(", statisticsTime=").append(statisticsTime);
sb.append(", activeNumber=").append(activeNumber);
sb.append(", activationGrowth=").append(activationGrowth);
sb.append("]");
return sb.toString();
return "CustomerNetworkStatistics{" +
"id=" + id +
", customerId=" + customerId +
", customerName='" + customerName + '\'' +
", deviceType='" + deviceType + '\'' +
", type='" + type + '\'' +
", statisticsTime='" + statisticsTime + '\'' +
", onlineNumber=" + onlineNumber +
'}';
}
}
\ No newline at end of file
}
......@@ -14,7 +14,13 @@ public class ShipmentData {
private Integer goodsNum;
private Byte type;
private String deviceType;
private Integer customerId;
private String customerName;
private String version;
public Integer getId() {
return id;
......@@ -40,21 +46,35 @@ public class ShipmentData {
this.goodsNum = goodsNum;
}
public Byte getType() {
return type;
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public void setType(Byte type) {
this.type = type;
public String getVersion() {
return version;
}
@Override
public String toString() {
return "ShipmentData{" +
"id=" + id +
", goodsTime='" + goodsTime + '\'' +
", goodsNum=" + goodsNum +
", type=" + type +
'}';
public void setVersion(String version) {
this.version = version;
}
}
package cn.com.glsx.openplatform.mapper;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.ActivationData;
import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.com.glsx.openplatform.model.ShipmentData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
......@@ -17,9 +19,17 @@ import org.apache.ibatis.annotations.Param;
public interface ActivationDataMapper {
List<ActivationData> getActivationList();
int countByDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
int updateDeviceActivation(@Param("data") ActivationData data,
@Param("startTimeStr") String startTimeStr,
@Param("endTimeStr") String endTimeStr, @Param("num") int num);
List<ShipmentData> getShipmentList();
List<String> selectDeviceTypeList();
List<ShipmentData> selectShipmentByType(@Param("deviceType") String deviceType);
int updatePlatformDeviceActivation(@Param("device") PlatformDevice device,
@Param("startTime") Date startTime, @Param("endTime") Date endTime,
@Param("count") int count);
}
package cn.com.glsx.openplatform.mapper;
import cn.com.glsx.openplatform.model.CustomerActionStatistics;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.CustomerActionStatistics;
import cn.com.glsx.openplatform.model.PlatformDevice;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>Description:
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
......@@ -15,6 +17,18 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface CustomerActionStatisticsMapper {
List<String> selectMonthList();
List<CustomerActionStatistics> selectByMonth(@Param("month") String month);
Integer selectOnlineNumber(CustomerActionStatistics actionStatistics);
List<CustomerActionStatistics> selectByType(@Param("type") String type);
List<PlatformDevice> selectDeviceList(
@Param("actionStatistics") CustomerActionStatistics actionStatistics,
@Param("endDate") Date endDate, @Param("count") Integer count);
int deleteByPrimaryKey(Integer id);
int insert(CustomerActionStatistics record);
......@@ -36,4 +50,4 @@ public interface CustomerActionStatisticsMapper {
int updateBatchSelective(List<CustomerActionStatistics> list);
int batchInsert(@Param("list") List<CustomerActionStatistics> list);
}
\ No newline at end of file
}
package cn.com.glsx.openplatform.mapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>Description:
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
......@@ -15,6 +16,12 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface CustomerNetworkStatisticsMapper {
List<CustomerNetworkStatistics> selectByDate(@Param("startDate") String startDate,
@Param("endDate") String endDate);
List<CustomerNetworkStatistics> selectFromDeviceNetworkByDate(@Param("startDate") Date startDate,
@Param("endDate") Date endDate);
int deleteByPrimaryKey(Integer id);
int insert(CustomerNetworkStatistics record);
......@@ -36,4 +43,4 @@ public interface CustomerNetworkStatisticsMapper {
int updateBatchSelective(List<CustomerNetworkStatistics> list);
int batchInsert(@Param("list") List<CustomerNetworkStatistics> list);
}
\ No newline at end of file
}
......@@ -17,10 +17,14 @@ import org.apache.ibatis.annotations.Param;
@Mapper
public interface DeviceActionDetailMapper {
int countByMonth(@Param("startMonth") Date startMonth, @Param("endMonth") Date endMonth);
List<String> selectDeviceType();
List<PlatformDevice> selectByMonth(@Param("startMonth") Date startMonth,
@Param("endMonth") Date endMonth, @Param("count") Integer count);
int countByDate(@Param("startDate") Date startDate, @Param("endDate") Date endDate,
@Param("type") String type);
List<PlatformDevice> selectByDate(@Param("startDate") Date startDate,
@Param("endDate") Date endDate, @Param("count") Integer count,
@Param("type") String type);
int deleteByPrimaryKey(Integer id);
......
......@@ -19,6 +19,19 @@
order by month;
</select>
<select id="countByDate" resultType="integer">
select count(1)
from t_platform_device
<where>
<if test="startDate != null">
and create_time >= #{startDate,jdbcType=TIMESTAMP}
</if>
<if test="endDate != null">
and create_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
</where>
</select>
<update id="updateDeviceActivation">
update t_platform_device
set customer_id = #{data.customerId,jdbcType=INTEGER},
......@@ -38,8 +51,31 @@
limit #{num,jdbcType=INTEGER}
</update>
<select id="getShipmentList" resultType="cn.com.glsx.openplatform.model.ShipmentData">
select id, goods_time goodsTime, goods_num goodsNum, type
<select id="selectDeviceTypeList" resultType="java.lang.String">
select distinct device_type
from t_shipment_data
</select>
<select id="selectShipmentByType" resultType="cn.com.glsx.openplatform.model.ShipmentData">
select id, goods_time goodsTime, goods_num goodsNum, device_type deviceType,
customer_id customerId, customer_name customerName, version
from t_shipment_data
<where>
<if test="deviceType != null and deviceType != ''">
and device_type = #{deviceType,jdbcType=VARCHAR}
</if>
</where>
</select>
<update id="updatePlatformDeviceActivation">
update t_platform_device
set activat_time = from_unixtime(
#{startTime,jdbcType=TIMESTAMP}
+ floor(rand() * (#{endTime,jdbcType=TIMESTAMP} - #{startTime,jdbcType=TIMESTAMP}))
)
where customer_id = #{device.customerId,jdbcType=INTEGER}
and device_type = #{device.deviceType,jdbcType=VARCHAR}
order by rand()
limit #{count}
</update>
</mapper>
......@@ -16,32 +16,45 @@
id, device_sn, `type`, create_time
</sql>
<select id="countByMonth" resultType="int">
<select id="selectDeviceType" resultType="java.lang.String">
select distinct device_type
from t_platform_device
where device_type != '通信模组'
</select>
<select id="countByDate" resultType="int">
select count(1)
from t_platform_device
<where>
<if test="startMonth != null">
and activat_time >= #{startMonth,jdbcType=TIMESTAMP}
<if test="startDate != null">
and activat_time >= #{startDate,jdbcType=TIMESTAMP}
</if>
<if test="endMonth != null">
and activat_time &lt; #{endMonth,jdbcType=TIMESTAMP}
<if test="endDate != null">
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
<if test="type != null and type != ''">
and device_type = #{type,jdbcType=VARCHAR}
</if>
</where>
</select>
<select id="selectByMonth" resultType="cn.com.glsx.openplatform.model.PlatformDevice">
<select id="selectByDate" resultType="cn.com.glsx.openplatform.model.PlatformDevice">
select id, device_sn deviceSn, device_type deviceType, customer_id customerId,
user_id userId, activat_time activatTime, status, create_time createTime,
update_time updateTime, create_by createBy, update_by updateBy, remark
from t_platform_device
<where>
<if test="startMonth != null">
and activat_time >= #{startMonth,jdbcType=TIMESTAMP}
<if test="startDate != null">
and activat_time >= #{startDate,jdbcType=TIMESTAMP}
</if>
<if test="endDate != null">
and activat_time &lt; #{endDate,jdbcType=TIMESTAMP}
</if>
<if test="endMonth != null">
and activat_time &lt; #{endMonth,jdbcType=TIMESTAMP}
<if test="type != null and type != ''">
and device_type = #{type,jdbcType=VARCHAR}
</if>
</where>
order by rand()
<if test="count != null">
limit #{count,jdbcType=INTEGER}
</if>
......
package cn.com.glsx.openplatform;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import cn.com.glsx.openplatform.mapper.CustomerActionStatisticsMapper;
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.hutool.core.util.RandomUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-02
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
public class GenerateCustomerActionStatistic {
@Autowired
private CustomerActionStatisticsMapper customerActionStatisticsMapper;
@Autowired
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
private BufferedWriter customerActionStatisticsWriter;
@Test
public void generateData() {
this.initCustomerActionStatisticWriter();
List<CustomerNetworkStatistics> networkStatisticsList = this.customerNetworkStatisticsMapper
.selectByDate(null, null);
for (CustomerNetworkStatistics networkStatistics : networkStatisticsList) {
Integer onlineNumber = networkStatistics.getOnlineNumber();
this.addCustomerActionStatisticsRecord(networkStatistics, "声控调用",
this.getRandomDouble(onlineNumber, 5, 9));
this.addCustomerActionStatisticsRecord(networkStatistics, "收音机使用",
this.getRandomDouble(onlineNumber, 28, 33));
this.addCustomerActionStatisticsRecord(networkStatistics, "导航调用",
this.getRandomDouble(onlineNumber, 3, 6));
this.addCustomerActionStatisticsRecord(networkStatistics, "音乐使用",
this.getRandomDouble(onlineNumber, 13, 18));
this.addCustomerActionStatisticsRecord(networkStatistics, "联网应用使用",
this.getRandomDouble(onlineNumber, 2, 4));
}
this.closeCustomerActionStatisticsWriter();
}
private void initCustomerActionStatisticWriter() {
try {
this.customerActionStatisticsWriter = Files.newBufferedWriter(
Paths.get("D:/Users/wbh/Desktop/customer-action-statistics.sql"));
this.customerActionStatisticsWriter.write(
"insert into t_customer_action_statistics " +
"(customer_id, customer_name, device_type, type, statistics_time, number) values");
} catch (IOException e) {
e.printStackTrace();
}
}
private void closeCustomerActionStatisticsWriter() {
try {
this.customerActionStatisticsWriter.flush();
this.customerActionStatisticsWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private int getRandomDouble(int base, int min, int max) {
return (int) (((double) base) * RandomUtil.randomDouble(min, max));
}
private void addCustomerActionStatisticsRecord(CustomerNetworkStatistics networkStatistics,
String type, int count) {
try {
String s = String.format(",\n(%d, '%s', '%s', '%s', '%s', %d)",
networkStatistics.getCustomerId(), networkStatistics.getCustomerName(),
networkStatistics.getDeviceType(),
type, networkStatistics.getStatisticsTime(), count);
this.customerActionStatisticsWriter.write(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cn.com.glsx.openplatform;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import cn.com.glsx.openplatform.mapper.CustomerNetworkStatisticsMapper;
import cn.com.glsx.openplatform.model.CustomerNetworkStatistics;
import cn.hutool.core.date.DateUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2021-01-02
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
public class GenerateCustomerNetworkStatistic {
@Autowired
private CustomerNetworkStatisticsMapper customerNetworkStatisticsMapper;
@Test
public void generateData() {
Date startDate = DateUtil.parse("2019-01-01", "yyyy-MM-dd");
Date endDate = DateUtil.parse("2021-01-01", "yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
calendar.add(Calendar.MONTH, 1);
Date currentStartDate = startDate;
Date currentEndDate = calendar.getTime();
while (currentStartDate.before(endDate)) {
List<CustomerNetworkStatistics> statisticsList = this.customerNetworkStatisticsMapper
.selectFromDeviceNetworkByDate(currentStartDate, currentEndDate);
if (!statisticsList.isEmpty()) {
this.customerNetworkStatisticsMapper.batchInsert(statisticsList);
}
currentStartDate = currentEndDate;
calendar.add(Calendar.MONTH, 1);
currentEndDate = calendar.getTime();
}
}
}
package cn.com.glsx.openplatform;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.com.glsx.openplatform.mapper.ActivationDataMapper;
import cn.com.glsx.openplatform.mapper.PlatformDeviceMapper;
import cn.com.glsx.openplatform.model.ActivationData;
import cn.com.glsx.openplatform.model.PlatformDevice;
import cn.com.glsx.openplatform.model.ShipmentData;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* <p>Description:
* <p>Project: rpc-openplatform-container
*
* @author wbh
* @date 2020-12-29
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class)
public class GenerateFakeData {
private static final Map<Byte, String> TYPE_MAP = new HashMap<>();
static {
TYPE_MAP.put((byte) 1, "通信模组");
TYPE_MAP.put((byte) 2, "核心板");
TYPE_MAP.put((byte) 3, "PCBA");
}
@Autowired
private ActivationDataMapper activationDataMapper;
@Autowired
private PlatformDeviceMapper deviceMapper;
@Test
public void test1() {
List<ShipmentData> shipmentList = activationDataMapper.getShipmentList();
for (ShipmentData shipmentData : shipmentList) {
String goodsTime = shipmentData.getGoodsTime();
int goodsNum = shipmentData.getGoodsNum() + RandomUtil.randomInt(1000, 2000);
Byte type = shipmentData.getType();
DateTime startTime = DateUtil.parse(goodsTime, "yyyy-MM-dd");
while (goodsNum > 0) {
int serialNum = 0;
int year = startTime.getField(DateField.YEAR);
int month = startTime.getField(DateField.MONTH);
int day = startTime.getField(DateField.DAY_OF_MONTH);
startTime = startTime.setField(DateField.HOUR_OF_DAY, RandomUtil.randomInt(24))
.setField(DateField.MINUTE, RandomUtil.randomInt(60))
.setField(DateField.SECOND, RandomUtil.randomInt(60));
int currentNum = goodsNum > 5000 ? RandomUtil.randomInt(4000, 5000) : goodsNum;
for (int i = 0; i < currentNum; i++) {
String sn = String.format("%d0%02d%02d%02d%04d", type, year % 100, month, day, serialNum);
PlatformDevice record = new PlatformDevice();
record.setDeviceSn(sn);
record.setDeviceType(TYPE_MAP.get(type));
record.setStatus(1);
record.setCreateTime(startTime);
record.setUpdateTime(record.getCreateTime());
record.setCreateBy(0);
record.setUpdateBy(0);
deviceMapper.insert(record);
serialNum++;
}
goodsNum -= currentNum;
startTime = startTime.offset(DateField.DAY_OF_MONTH, -1);
}
}
}
@Test
public void dataHanlder() throws Exception {
//查询数据库
List<ActivationData> activationList = activationDataMapper.getActivationList();
for (ActivationData activationData : activationList) {
activationData.setTypeName(TYPE_MAP.get(activationData.getType()));
String month = activationData.getMonth();
activationData.setEndMonth(getCurrentMonthLastDay(month) + " 23:59:59");
//String currentMonth = month;
//根据obj中month字段获取上月第一天和最后一天
int num = activationData.getNum(); //obj中number
//int real = 0;
//int time = 1;
//do {
String startTime = getPreMonthOneDay(month, -9);
String endTime = getPreMonthLastDay(month, -1);
//currentMonth = startTime;
int j = activationDataMapper.updateDeviceActivation(activationData,
startTime + " 00:00:00",
endTime + " 23:59:59", num);
//real += j;
//time--;
//} while (real < num && time > 0);
}
}
/**
* 从当前时间获取上个月的第一天
*/
private String getPreMonthOneDay(String startDate, int num) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = getPreMonth(startDate, num);
//获取某月最小天数
int firstDay = c.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
c.set(Calendar.DAY_OF_MONTH, firstDay);
// 上个月第一天
String startTime = format.format(c.getTime());
return startTime;
}
/**
* 从当前时间获取上个月的最后一天
*/
private String getPreMonthLastDay(String startDate, int num) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c2 = getPreMonth(startDate, num);
int lastDay = c2.getActualMaximum(Calendar.DAY_OF_MONTH);
c2.set(Calendar.DAY_OF_MONTH, lastDay);
String endTime = format.format(c2.getTime());
return endTime;
}
private String getCurrentMonthLastDay(String month) throws Exception{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = format.parse(month);
c.setTime(date);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, lastDay);
String endTime = format.format(c.getTime());
return endTime;
}
/**
* 从当前时间"yyyy-MM-dd"格式获取上个月的时间
*/
private Calendar getPreMonth(String startDate, int num) throws Exception{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date = format.parse(startDate);
c.setTime(date);
c.add(Calendar.MONTH, num);
return c;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!