java后端笔记

曲线日期工具类

package com.iesms.openservices.base.util;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author lzx
* @version 1.0
* @date 2021/2/1 9:30
*/
public class CurveUtil {
public static <T> List<String> getValues(T model, String name, int acc, int endIndex) {
if (model == null) {
return null;
}
Class<?> aClass = model.getClass();
List<String> result = new ArrayList<>();
for (int i = 1; i < endIndex; i++) {
String name2 = name + String.format("%02d", i);
try {
Field field = aClass.getDeclaredField(name2);
if (field == null) {
result.add("");
} else {
field.setAccessible(true);
Object value = field.get(model);
if (value == null) {
result.add("");
} else {
String toString = value.toString();
toString = stringAccuracy(toString, acc);
result.add(toString);
}
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
return result;
}
public static String stringAccuracy(String value, int acc) {
if (value == null) {
return null;
}
if (value.equals("0E-8")) {
value = "0.00000000";
}
if (value.length() > acc + 1) {
value = value.substring(0, value.indexOf(".") + acc + 1);
}
// 去掉末尾多余的0
value = subZeroAndDot(value);
return value;
}
public static String subZeroAndDot(String s) {
if (s == null) {
return null;
}
if (s.indexOf(".") > 0) {
// 去掉多余的0
s = s.replaceAll("0+?$", "");
// 如最后一位是.则去掉
s = s.replaceAll("[.]$", "");
}
return s;
}
/**
* Description: 生成曲线时间
*
* @param length: 时间点位
* @param step: 每次增长的时长
* @author: ghl
* @return: java.util.List<java.lang.String>
* @date: 2020/4/28 20:14
*/
public static String[] getDayLables(int length, int step) {
try {
String[] strings = new String[length];
String newTime = "00:00";
strings[0] = newTime;
for (int i = 1; i < length; i++) {
newTime = timeAddStep(newTime, step);
strings[i] = newTime;
}
return strings;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Description: 处理时间变量
*
* @param oldTime: 原时间
* @param step: 处理时间时长
* @author: ghl
* @return: java.lang.String
* @date: 2020/4/28 20:15
*/
public static String timeAddStep(String oldTime, int step) {
try {
int addMit = step;
DateFormat df = new SimpleDateFormat("HH:mm");
Date date = null;
date = df.parse(oldTime);
Date expireTime = new Date(date.getTime() + addMit * 60 * 1000);
String newTime = df.format(expireTime);
return newTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
*
* @param year
* @param month
* @return
*/
public static List<String> getMonthFullDay(int year, int month) {
SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
List<String> fullDayList = new ArrayList<>(32);
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month - 1);
// 当月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count; j++) {
fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return fullDayList;
}
/**
* 获取某年所有的月份(格式:yyyyMM)
*
* @return
*/
public static List<String> getMonthsByYearForMonth(Integer year) {
List<String> list = new ArrayList<String>();
for (int i = 1; i <= 12; i++) {
list.add(String.valueOf(i));
}
return list;
}
/**
* 获取 获取某年某月 所有日期(dd格式字符串)
*
* @param year
* @param month
* @return
*/
public static List<String> getMonthFullDayForDay(int year, int month) {
SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("dd");
List<String> fullDayList = new ArrayList<>(32);
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month - 1);
// 当月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count; j++) {
fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH, 1);
}
return fullDayList;
}
/**
* 获取某年所有的月份(格式:yyyyMM)
*
* @return
*/
public static List<String> getMonthsByYear(Integer year) {
List<String> list = new ArrayList<String>();
for (int i = 1; i <= 12; i++) {
switch (i) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
list.add(year + "0" + i);
break;
default:
list.add(year + "" + i);
break;
}
}
return list;
}
/**
* 给月份去符号 如2020-03 返回202003
*
* @param dataStr
* @return
*/
public static String removeFormatDataMonth(String dataStr) {
String newDate = dataStr.substring(0, 4) + dataStr.substring(5, 7);
return newDate;
}
/**
* 给月份加符号 如202003 返回2020-03
*
* @param dataStr
* @return
*/
public static String formatDataMonth(String dataStr) {
String newDate = dataStr.substring(0, 4) + "-" + dataStr.substring(4, 6);
return newDate;
}
/**
* 给月份集合加符号 如202003 返回2020-03
*
* @param labels
* @return
*/
public static List<String> formatDataMonthList(List<String> labels) {
List<String> list = new ArrayList<>();
for (int i = 0; i < labels.size(); i++) {
String month = formatDataMonth(labels.get(i));
list.add(month);
}
return list;
}
/**
* 根据开始时间和结束时间返回时间段内的时间集合[01, 02, 03 …… 31]
*
* @param beginDate
* @param endDate
* @return List
*/
public static List<String> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
Calendar cal = Calendar.getInstance();
List<String> lDate = new ArrayList<>();
cal.setTime(beginDate);
lDate.add(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)));// 把开始时间加入集合
boolean bContinue = true;
while (bContinue) {
//TODO 根据日历的规则,为给定的日历字段添加或减去指定的时间量
cal.add(Calendar.DAY_OF_MONTH, 1);
//TODO 测试此日期是否在指定日期之后
if (endDate.after(cal.getTime())) {
lDate.add(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)));
} else {
break;
}
}
cal.setTime(endDate);
lDate.add(String.format("%02d", cal.get(Calendar.DAY_OF_MONTH)));// 把结束时间加入集合
System.out.println(lDate);
return lDate;
}
/**
* 根据开始时间,结束时间获取期间所有日期yyyy-MM-dd
*
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getDatesBetweenTwoDateFormat(Date beginDate, Date endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> allDate = new ArrayList();
allDate.add(sdf.format(beginDate));
Calendar calBegin = Calendar.getInstance();
//TODO 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(beginDate);
Calendar calEnd = Calendar.getInstance();
//TODO 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(endDate);
//TODO 测试此日期是否在指定日期之后
while (endDate.after(calBegin.getTime())) {
//TODO 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
allDate.add(sdf.format(calBegin.getTime()));
}
return allDate;
}
/***
* 获取两个月份时间段的月份集合 [202101, 202102, 202103, 202104, 202105]
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getDatesBetweenTwoMonths(String beginDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date start = sdf.parse(beginDate);
Date end = sdf.parse(endDate);
List<String> allDate = new ArrayList();
Calendar calBegin = Calendar.getInstance();
allDate.add(beginDate);
//TODO 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(start);
//TODO 测试此日期是否在指定日期之后
while (end.after(calBegin.getTime())) {
//TODO 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.MONTH, 1);
allDate.add(sdf.format(calBegin.getTime()));
}
return allDate;
}
}

定时任务:https://www.cnblogs.com/2016-10-10/p/6283321.html

导出Excel版本问题 https://blog.csdn.net/qq_38218238/article/details/89311443

java 四舍五入保留两位小数,小数点后无用的零去掉

NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(3.70000));
去掉末尾0:
BigDecimal bd = new BigDecimal("1245600.000");
System.out.println(bd.stripTrailingZeros());

java前面补零

// 0 代表前面补充0
// 8 代表长度为8
// d 代表参数为正数型
return String.format("%08d", sequence);
//自动生成模板编号(往后加一)
String pre = "EVT";
TmplBillingDo maxTmplNo = tmplBillingDao.getMaxTmplNo();
if (maxTmplNo == null || StrUtil.isBlank(maxTmplNo.getTmplNo())) {
maxTmplNo.setTmplNo(pre + "00001");
} else {
String tmplNo = maxTmplNo.getTmplNo();
//去除模板编号的前缀在加一 (removePrefix)
String format = String.format("%05d", (Long.parseLong(StrUtil.removePrefix(tmplNo, pre)) + 1));
maxTmplNo.setTmplNo(pre + format);
}
return maxTmplNo;

当数据库表的某个字段类型为char()时,应当注意其自动填充空格补齐位数的特性。(trim)

select count(*) as NUM from TB_A where trim(data_no) = ?

从一个集合里面删除所有值为a的所有元素。

List<String> stringList = new ArrayList<>();
stringList.add("abc");
stringList.add("apple");
stringList.add("blue");
stringList.add("yellow");
stringList.add("api");
stringList.add("liu");
stringList.removeIf(s -> s.contains("a"));
System.out.println("stringList:" + stringList); //stringList:[blue, yellow, liu]

15分钟一个点 96个点曲线数据

/**
* 15分钟一个点 96个点曲线数据
*/
public class JavaFor96 {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("00:00", "00:15", "00:30", "00:45", "23:45");
List<String> minuteList = getFifteenMinute();
List<String> list = new ArrayList<>();
for (int i = 0, j = 0; i < stringList.size(); i++, j++) {
if (j > 96) break;
if (minuteList.get(j).equals(stringList.get(i))) {
list.add(minuteList.get(j));
} else {
i--;
list.add(null);
}
}
System.out.println(minuteList);
System.out.println(list);
}
private static String getDate(int amount) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
/*calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);*/
calendar.set(Calendar.MINUTE, amount);
return sdf.format(calendar.getTime());
}
/**
* [00:00, 00:15, ……, 23:45] 15分钟一个点,一共96个点
*/
private static List<String> getFifteenMinute() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 96; i++) {
list.add(getDate(i * 15));
}
return list;
}
}

获取查询json字段里的List(数组)属性值

JSONObject jsonObject = JSONUtil.parseObj(mbShareManagementDto.getShareTaskParams());
JSONArray meterList = jsonObject.getJSONArray("econs_consume_meter_list");
if (ObjectUtil.isNotEmpty(meterList)) {
mbShareManagementDto.setEconsConsumeMeterList(meterList.toList(String.class));
}

循环去掉String类型后面的0 sql语句用like匹配(前端传参区域编码)

String adCode = "3450000";
if (StrUtil.isNotBlank(adCode)) {
while (adCode.endsWith("0")) {
adCode = adCode.substring(0, adCode.length() - 1);
}
}
System.out.println(adCode);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值