工作上的积累
由于自己的不熟练导致出有很多问题一下是自己的问题积累
java
String
-
字符串int之间的转换
Integer.valueOf(total) 字符串转int -
格林威治时间(Tue Jan 01 00:00:00 CST 2019)[ Date ]转化 为 [ 2019-01-01 10:10:10 ]
解决办法:
String date = “Tue Jan 01 00:00:00 CST 2019”;
SimpleDateFormat sdf = new SimpleDateFormat(“EEE MMM dd HH:mm:ss z yyyy”,Locale.US);
Date d=sdf.parse(date);
sdf=new SimpleDateFormat(“yyyyMMdd”); -
string.contains("+") 在map中 containsKey是否包含 包含这个单词很重要
-
关于字符串的截取
String strhours = String.valueOf(123456);
String strh = strhours.substring(strhours.length() -2,strhours.length()); //截取
String strm = strhours.substring(0,strhours.length()-2); //截掉
strh内容为 56 strm内容为 1234 -
字符串与时间的转换
时间转换 timestemp 转date
SimpleDateFormat format = new SimpleDateFormat(“yyyy-M-dd HH:mm:ss”);
format.format(calendar.getTime()) 转成字符串
format.perse() 转成Date对象 -
定时任务的注解
@EnableScheduling // 2.开启定时任务
@Scheduled(cron = “0/30 * * * * ?”) 每30秒执行一次 -
Jsoup 类似于爬虫
-
日历类
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.parseInt(yearTime)); 年
calendar.set(Calendar.MONTH, Integer.valueOf(month) - 1); 月份 是 0-11 所以传入数值要减一
calendar.set(Calendar.DATE, Integer.valueOf(split[3])); 日
calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(InfoList1.get(0))); 小时
calendar.set(Calendar.MINUTE, Integer.valueOf(InfoList1.get(1))); 分钟
calendar.set(Calendar.SECOND, 00); 秒 -
Java定时调度机制 - ScheduledExecutorService
@Test public void test_scheduleWithFixedDelay() {
ScheduledExecutorService service = Executors.newScheduledThreadPool(5); 第二步
service.scheduleWithFixedDelay(() -> {
try {
Thread.sleep(3000L); 第三部
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("task finish time: " + format(System.currentTimeMillis()));
}, 1000L 第一次运行时间, 1000L 之后每一次调用时间, TimeUnit.MILLISECONDS);单位System.out.println("schedule finish time: " + format(System.currentTimeMillis())); 第一步
while (true) {
}
} -
随机数
Random rand = new Random();
int randNumber =rand.nextInt(60 - 30 + 1) + 30; -
转换
JSON.parse(); 转成json对象 -
把两个list合并成一个 重复数据不显示
方法:
Set set = new HashSet<>(listA); 第一种方法
set.addAll(listB);
List list = new ArrayList<>(set);
System.out.println(list);List collect = Stream.of(listA, listB) 第二种方法
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList()); -
字符串判断非空
判断字符串是否为空的几种方法
if (str != null || !"".equals(str.trim())) {} //则字符串不为空或空格
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null || s.equals(""));方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法: if(s == null || s.length() == 0);
方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二. if(s == null || s.isEmpty());
方法四: 这是一种比较直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多: if (s == null || s == “”);
-
StringUtils.isEmpty 和 StringUtils.isBlank 是有区别的
StringUtils 是lang 或者 lang3 包下的 -
日期格式转换
SimpleDateFormat format = new SimpleDateFormat(“yyyy/MM/dd”); 格式是自己决定呢
SimpleDateFormat parse = new SimpleDateFormat(“yyyy-MM-dd”);
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); -
map 是 list 的速度的50倍
-
long startTime = System.currentTimeMillis(); 当前时间戳
-
@Component:可以用于注册所有bean
-
注解效验值 注解形式对于类型不同 选择不同的校验方式
@NotEmpty 用在集合类上面
@NotBlank 用在String上面
@NotNull 用在基本类型上
@ApiModelProperty(value = “responseData”)
@NotEmpty(message=“responseData不能为空”)
private Map<String, Object> responseData;
@ApiModelProperty(value = "角色名")
@NotBlank(message="角色不能为空")
private String roleName;
@ApiParam()
@Data
@ApiModel(value = "合同状态回调信息")
public class ReceiveBestsignForm {
@ApiModelProperty(value = "type")
@NotBlank(message="type不能为空")
private String type;
-
toString 方法可以重写 生成自己想要的 方便自己以后解析
-
json的字符串可以转换成map对象 方便自己使用
Map<String, Object> map = JSONObject.parseObject(content); -
数组转成List
String[] contractIds = contractId.substring(1,contractId.length()-1).split(",");
List resultList= new ArrayList<>(Arrays.asList(contractIds)); -
有时候记得用trim去前后的空格 会发生的问题 会空指针
-
map 转json字符串 JSON.toJSON(List).String 变为json字符串
Map map = new HashMap();
//1、map里面装有msg键存有one值
map.put(“msg”, “one”);
//2、将map转成json对象
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println(“输出的结果是:” + jsonObject);
//3、将json对象转化为json字符串
String result = jsonObject.toString();
System.out.println(result);
Map<String, Object> params = new HashMap<>();
params.put("a","11111");
params.put("b","2222");
System.out.println(params);//{"a":"11111","b":"22222"}
String json = JSON.toJSONString(params);//map转json字符串
-
try(){}
try(){}自动释放资源,AutoCloseable -
Java防止科学计数法
public static String NonScientificNotation(String num)
{
Pattern pattern = Pattern.compile("-?[0-9]*.[0-9]E[0-9]");
Matcher match = null;
match = pattern.matcher(num);
if(match.matches())
{
BigDecimal decimal = new BigDecimal(num);
num = decimal.toPlainString();
}
return num;
}
xml
- 在xml中
jdbcType=DATE对应的是没有时分秒的
jdbcType=Timestamp 则有 - 如果参数类型是两个在xml中就不要写parameterType=""
- xml中的特殊符号
|< < 小于号
> > 大于号
& & 和
’ ’ 单引号
" " 双引号
xml中遍历LIst 默认是list 如果不行就在mapper中添加@param指定