package com.saite.energy.data.cache.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
@Slf4j
public class IdsMathUtil {
public static int BIG_DECIMAL_SCALE = 3;
public static BigDecimal toDecimal(String val) {
if (StringUtils.isBlank(val)) {
return null;
}
try {
return new BigDecimal(val);
} catch (Exception e) {
return null;
}
}
public static BigDecimal getBigDecimalByRedisObj(Object obj) {
if (obj == null) {
return null;
}
return toDecimal(obj.toString());
}
public static String getBigDecimalScale(Object value) {
if (value == null) {
return null;
}
String val = "";
if (value instanceof BigDecimal) {
val = ((BigDecimal)value).setScale(BIG_DECIMAL_SCALE, BigDecimal.ROUND_HALF_UP).toString();
} else if(value instanceof Float) {
val = new BigDecimal((Float)value).setScale(BIG_DECIMAL_SCALE, BigDecimal.ROUND_HALF_UP).toString();
} else if(value instanceof Double) {
val = new BigDecimal((Double) value).setScale(BIG_DECIMAL_SCALE, BigDecimal.ROUND_HALF_UP).toString();
} else {
val = value.toString();
}
return val;
}
public static Integer getIntegerByObject(Object obj) {
if (obj == null) {
log.warn("数据为空");
return null;
}
try {
return new BigDecimal(obj.toString()).intValue();
} catch (Exception e) {
log.error("数据转换错误");
return null;
}
}
}
package com.saite.energy.ids.utils;
import com.saite.energy.ids.enums.TimeDimEnum;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class TimeStampUtil {
public static Long dayTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long monthTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long yearTimeInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.DATE, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long yesterdayInMillis() {
return dayTimeInMillis() - 86400000;
}
public static Long beforeYesterdayInMillis() {
return dayTimeInMillis() - (86400000 * 2);
}
public static Long lastMonthInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long lastYearInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.YEAR, -1);
calendar.add(Calendar.DATE, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long getDayStartTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long getDayEndTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
}
public static Long getMonthStartTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long getMonthEndTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTimeInMillis();
}
public static Long getYearStartTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
calendar.add(Calendar.YEAR, 0);
calendar.add(Calendar.DATE, 0);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTimeInMillis();
}
public static Long getYearEndTime(Long timeStamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
calendar.setTimeInMillis(timeStamp);
int year = calendar.get(Calendar.YEAR);
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar.getTimeInMillis();
}
public static Long getStartTime(Long collectTime, String timeDim) {
Long startTime = null;
if ("year".equals(timeDim)) {
startTime = TimeStampUtil.getYearStartTime(collectTime);
} else if ("month".equals(timeDim)) {
startTime = TimeStampUtil.getMonthStartTime(collectTime);
} else {
startTime = TimeStampUtil.getDayStartTime(collectTime);
}
return startTime;
}
public static Long getEndTime(Long collectTime, String timeDim) {
Long endTime = null;
if ("year".equals(timeDim)) {
endTime = TimeStampUtil.getYearEndTime(collectTime);
} else if ("month".equals(timeDim)) {
endTime = TimeStampUtil.getMonthEndTime(collectTime);
} else {
endTime = TimeStampUtil.getDayEndTime(collectTime);
}
return endTime;
}
public static String getDateStrByTimeDim(Long timeStamp, String timeDim) {
if ("year".equals(timeDim)) {
return new SimpleDateFormat("yyyy年", Locale.CHINA).format(new Date(timeStamp));
} else if ("month".equals(timeDim)) {
return new SimpleDateFormat("yyyy年MM月", Locale.CHINA).format(new Date(timeStamp));
} else {
return new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA).format(new Date(timeStamp));
}
}
public static String getDateStrByTimeStamp(Long timeStamp) {
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date(timeStamp));
return date;
}
public static Long currentInMillis() {
Calendar calendar = Calendar.getInstance();
return calendar.getTimeInMillis();
}
public static Long lastWeekInMillis() {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH) - 7, 0, 0, 0);
return calendar.getTimeInMillis();
}
public static Long getStartTimeByDim(Long time, String timeDim) {
if (TimeDimEnum.DAY.getTimeDim().equals(timeDim)) {
return getDayStartTime(time);
}
if (TimeDimEnum.MONTH.getTimeDim().equals(timeDim)) {
return getMonthStartTime(time);
}
if (TimeDimEnum.YEAR.getTimeDim().equals(timeDim)) {
return getYearStartTime(time);
}
if (TimeDimEnum.ALL.getTimeDim().equals(timeDim)){
return yearTimeInMillis();
}
return 0L;
}
public static Long getEndTimeByDim(Long time, String timeDim) {
if (TimeDimEnum.DAY.getTimeDim().equals(timeDim)) {
return getDayEndTime(time);
}
if (TimeDimEnum.MONTH.getTimeDim().equals(timeDim)) {
return getMonthEndTime(time);
}
if (TimeDimEnum.YEAR.getTimeDim().equals(timeDim)) {
return getYearEndTime(time);
}
return 0L;
}
public static String getDateStrByTimeDimAndStationCode(Long timeStamp, String timeDim, String stationCode) {
String date = "";
if (StringUtils.isBlank(stationCode)) {
if ("year".equals(timeDim)) {
date = new SimpleDateFormat("yyyy", Locale.CHINA).format(new Date(timeStamp));
} else if ("month".equals(timeDim)) {
date = new SimpleDateFormat("yyyy-MM", Locale.CHINA).format(new Date(timeStamp));
} else {
date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date(timeStamp));
}
} else {
if ("year".equals(timeDim)) {
date = new SimpleDateFormat("yyyy-MM", Locale.CHINA).format(new Date(timeStamp));
} else if ("month".equals(timeDim)) {
date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date(timeStamp));
} else {
date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(new Date(timeStamp));
}
}
return date;
}
public static Long getTimeStampMilliByLocalDateTime(LocalDateTime localDateTime) {
long l = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
return l;
}
public static Long getTimeStampSecondByLocalDateTime(LocalDateTime localDateTime) {
long l = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
return l;
}
public static double getDayNumBetween(Long sLong,Long mLong){
double dayNum = (double)(mLong-sLong)/24/3600/1000;
return dayNum;
}
public static Long dateTimeStamp(String date_str,String format){
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(date_str).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}