自学随笔1

 1.startWith:以某某字符串开始 

   endWith:   以某某字符串结束

2.在接收的字段上进行日期格式化

@DateTimeFormat(pattern=“yyyy-MM-dd HH:mm:ss”) 表示从前台传入后台的字符串规则 将其 转为日期

@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss” )表示接口返回的string日期 按照 这个规则 格式化字符串

一般这个两个都是同时使用,保证传入@JsonFormat的字符串格式 与 @DateTimeFormat一致

一般这个两个都是同时使用,保证传入@JsonFormat的字符串格式 与 @DateTimeFormat一致

 	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss")
    private Date date;
 
    public void setDate(Date date){
        this.date = date;
    }
    public Date getDate(){
        return date;
    }

 3.全局时间格式化

首先,我们找到 Spring Boot 的配置文件 application.properties(或 application.yml),只需要在 application.properties 配置文件中添加以下两行配置:

1.格式化全局时间字段,在yml中添加如下配置:

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

2.指定时间区域类型,在yml中添加:

spring.jackson.time-zone=GMT+8
spring:
	jackson:
		## 格式为yyyy-MM-dd HH:mm:ss
	    date-format: yyyy-MM-dd HH:mm:ss
	    ## 定时区,解决8小时的时间差问题
	    time-zone: GMT+8

4..部分时间格式化(推荐)

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class User {
    private int id;
    private String username;
    // 对 createtime 字段进行格式化处理
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss", timezone = "GMT+8")
    private Date createtime;
    private Date updatetime;
}

5.substring(起始索引,结束索引)0,不包含结束索引

public String substring(int beginIndex, int endIndex) {
        int length = length();
        checkBoundsBeginEnd(beginIndex, endIndex, length);
        if (beginIndex == 0 && endIndex == length) {
            return this;
        }
        int subLen = endIndex - beginIndex;
        return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
                          : StringUTF16.newString(value, beginIndex, subLen);
    }

6.Indexof

indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

Java中字符串中子串的查找共有四种方法,如下:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。 
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。 
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

public class Test {  
    public static void main(String[] args) {  
        String s = "xXccxxxXX";  
        // 从头开始查找是否存在指定的字符         //结果如下   
        System.out.println(s.indexOf("c"));     //2  
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(s.indexOf("c", 3));  //3  
        //若指定字符串中没有该字符则系统返回-1  
        System.out.println(s.indexOf("y"));     //-1  
        System.out.println(s.lastIndexOf("x")); //6  
    }  
} 

7.字符串的拼接

public static void main(String[] args) {
        /*String path = "D:\\ProjectDatas\\HighAltitudeWindResourceAnalysisPredictionPlatform\\RemoteSftpFile\\home\\t51184\\WRF\\WRF-4.1\\run_opr\\202211120000\\wrfout_d01_2022-11-13_01";
        int start = path.indexOf("\\") + 1;
        for(int i = 0;; i++) {
            int end = path.indexOf("\\", start+1);
            if(path.substring(start, end).equals("file")) {
                break;
            }
            start = end + 1;
        }
        path = path.substring(start - 1);
        System.out.println(path);
*/

        String s1 = "wrfout_d01_2022-04-29_00_00_00_wswd_T_RH_PHB_H.nc";
        String s = s1.substring(s1.indexOf("1")+2, s1.indexOf("_wswd_T_RH_PHB_H.nc"));
        System.out.println(s);
    }

8.日期的增减

(1)日期格式的转换包括对时分秒的添加和减少

public static void main(String[] args) {

        String s1 = "wrfout_d01_2022-04-29_00_00_00_wswd_T_RH_PHB_H.nc";
        String s = s1.substring(s1.indexOf("1")+2, s1.indexOf("_wswd_T_RH_PHB_H.nc"));
        System.out.println(s);

        String format = LocalDateTime.parse(s, DateTimeFormatter.ofPattern(String.format("yyyy-MM-dd_HH_mm_ss"))).minusMinutes(10).format(DateTimeFormatter.ofPattern(String.format("yyyy-MM-dd HH:mm:ss")));
        System.out.println(format);
    }


//运行结果
2022-04-29_00_00_00
2022-04-28 23:50:00

(2)对当前时间的向后或者向前推几天

 for (int i = 0; i < 4; i++) {
            String format = LocalDate.now().plusDays(i).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            System.out.println(format);
        }

9.文件以及文件夹的问题

a.Files 是用于操作文件或目录的工具类.

描述方法
文件的复制Path copy(Path src, Path dest, CopyOption … how)
创建一个目录Path createDirectory(Path path, FileAttribute<?> … attr)
创建一个文件Path createFile(Path path, FileAttribute<?> … arr)
删除一个文件void delete(Path path)
将 src 移动到 dest 位置Path move(Path src, Path dest, CopyOption…how) :
返回 path 指定文件的大小long size(Path path)

b.Files常用方法

用于判断

boolean exists(Path path, LinkOption … opts) : 判断文件是否存在
boolean isDirectory(Path path, LinkOption … opts) : 判断是否是目录
boolean isExecutable(Path path) : 判断是否是可执行文件
boolean isHidden(Path path) : 判断是否是隐藏文件
boolean isReadable(Path path) : 判断文件是否可读
boolean isWritable(Path path) : 判断文件是否可写
boolean notExists(Path path, LinkOption … opts) : 判断文件是否不存在

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
 
public class FilesTest {
 
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Path path = Paths.get("src/notes/io/FilesTest.java");
		// 复制文件
		Files.copy(path, new FileOutputStream("src/notes/io/CopyFilesTest.txt"));
		// 判断FilesTest.java是不是隐藏文件
		System.out.println("FilesTest.java是隐藏文件么?" + Files.isHidden(path));
		// 一次性读取FilesTest.java文件的所有行
		List<String> lines = Files.readAllLines(path);
		System.out.println(lines);
		// 判断文件的大小
		System.out.println("FilesTest.java的文件大小为" + Files.size(path));
		List<String> poem = new ArrayList<>();
		poem.add("野火烧不尽");
		poem.add("春风吹又生");
		// 直接将多个字符串内容写入到指定文件中
		Files.write(Paths.get("src/notes/io/CopyFilesTest.txt"), poem, Charset.forName("utf-8"));
	}
 
}

10.判断某个属性是否为空值可以使用Objects.isNull(initialTimeStr)可以进行判断

11.@Cacheable 注解:springboot缓存注解使用

12.String.format()的使用

    String str=null;  
    str=String.format("Hi,%s", "siri");   
    System.out.println(str);   //Hi,siri
    
    str=String.format("Hi,%s %s", "siri","我在");            
    System.out.println(str);                 //Hi,siri 我在 
              
    System.out.printf("字母c的大写是:%c %n", 'C');  //字母c的大写是:C  
    System.out.printf("布尔结果是:%b %n", "小超".equal("帅哥"));  //布尔的结果是:false   
    System.out.printf("100的一半是:%d %n", 100/2);  //100的一半是:50   
    System.out.printf("100的16进制数是:%x %n", 100);  //100的16进制数是:64   
    System.out.printf("100的8进制数是:%o %n", 100);  //100的8进制数是:144   
    System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85);  //50元的书打8.5折扣是:42.500000 元  
    System.out.printf("上面价格的16进制数是:%a %n", 50*0.85);  //上面价格的16进制数是:0x1.54p5   
    System.out.printf("上面价格的指数表示:%e %n", 50*0.85);  //上面价格的指数表示:4.250000e+01   
    System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85);  //上面价格的指数和浮点数结果的长度较短的是:42.5000 
    System.out.printf("上面的折扣是%d%% %n", 85);  //上面的折扣是85% 
    System.out.printf("字母A的散列码是:%h %n", 'A');    //字母A的散列码是:41   

13.Files.walkFileTree() 遍历文件,通过筛选找到相应条件的文件,

Files.walkFileTree(Paths.get(childPath), new SimpleFileVisitor<>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {

                if (file.getFileName().toString().endsWith(suffix)) {
                    files.add(String.format("%s\\%s", childPath, file.getFileName()));
                    return FileVisitResult.CONTINUE;
                }

                return FileVisitResult.CONTINUE;
            }
        });



 Files.walkFileTree(Paths.get(everyCounty), new SimpleFileVisitor<>(){
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                if (!pmgPathMatcher.matches(file)) {
                    return FileVisitResult.CONTINUE;
                }

                InfoReport infoReport = new InfoReport();
                infoReport.setModifyTime(LocalDateTime.ofInstant(attrs.lastModifiedTime().toInstant(), ZoneId.systemDefault()));
                infoReport.setFilename(file.getFileName().toString());
                infoReport.setFilePath(String.format("%s\\%s", everyCounty, infoReport.getFilename()).replace(parentPath, url));
                infoReports.add(infoReport);
                return FileVisitResult.CONTINUE;
            }
        });

14.当出现返回返回结果的实体类当中只有泛型不一致,而其它字段一致的时候可以这样使用

@Data
public class RealDTO<T> {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime realTime;
    private List<T> stations;
    private List<List<Double>> values;
}

比较

@Data
public class ColorDataDTO {

    private List<TempStations> tempData;

    private List<List<Double>> values;
}


@Data
public class ColorRainDataDTO {

    private List<RainStations> tempData;

    private List<List<Double>> values;
}

15.数组利用Arrays进行从大到小的顺序进行排序

Arrays.stream(arr).sorted(Comparator.reverseOrder()).findFirst().orElse(null);

16.

 17.

public Page<Merchants> shopList(MerchantRequest info) {
        LambdaQueryWrapper<Merchant> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊搜索条件
        if (StringUtils.notEmpty(info.getKeyword())) {
            queryWrapper.and(c -> c.like(Merchant::getMallName, info.getKeyword())
                    .or().like(Merchant::getOwnerId, info.getKeyword())
                    .or().like(Merchant::getOwnerName, info.getKeyword()));
        }
        // 时间范围查询
        if (StringUtils.notEmpty(info.getCreateTimeStart()) && StringUtils.notEmpty(info.getCreateTimeEnd())) {
            queryWrapper.ge(Merchant::getCreateTime, info.getCreateTimeStart());
            queryWrapper.le(Merchant::getCreateTime, info.getCreateTimeEnd());
        }
		// 排序条件
        if (StringUtils.notEmpty(info.getSort()) && StringUtils.notEmpty(info.getOrder())) {
            // sort ---> "create_time" , order -> desc,asc
            queryWrapper.last(" order by ".concat(info.getSort()).concat(" ").concat(info.getOrder()));
        }

        return merchantService.page(new Page<>(info.getPage(), info.getSize()), queryWrapper);
    }

判断字符串是否为空可以使用StringUntils的工具类

18.字符串转换为LocalDateTime的指定的格式

LocalDateTime.parse(endTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))

19.接收参数接收为指定的日期的格式所用到的注解

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime

20.stringutils.hastext()判断字符串是否为空,为空则返回Fasle,不为空返回ture

21.多级菜单展示效果

 a.根据用户id获取用户的所有菜单权限

 b.将获取的菜单权限和父级id作为参数去找他们的子菜单设置到children属性中

 c.获取存入参数的 子Menu集合

22.@RequiresPermissions注解用于权限验证

23.mysql中添加集合类型的数据:

<insert id="saveBatchInfo">
        insert into duty_infos (id,title,`start`,duty_type) values
              <foreach item="item" index="index" collection="list" separator=",">
        (#{item.id},#{item.title},#{item.start},#{item.dutyType})
    </foreach>
    </insert>

24.数据库表中列名与mysql中的关键字冲突可以使用符号``进行区分

25.concat函数返回结果为多个字符串拼接而成的新的字符串。如果有任何一个字符串为NULL,则返回为NULL。

26.集合遍历

List<String> list = new ArrayList<String>();
		list.add("张三");
		list.add("李四");
		list.add("王五");
		list.add("赵六");
		//方式一:foreach遍历
		for(String temp : list){
			System.out.print(temp+"\t");
		}
		//方式二:Iterator迭代器遍历
		Iterator it = list.iterator();
		while(it.hasNext()){
			Object obj = it.next();
			System.out.print(obj+"\t");
		}
		//方式三:for循环
		for(int i=0;i<list.size();i++){
			System.out.print(list.get(i)+"\t");
		}
Map<String, String> map = new HashMap<>();
		map.put("张三", "西安");
		map.put("赵四", "成都");
		map.put("王五", "杭州");
		map.put("老六", "北京");
		// 方式一:遍历map的键或者值
		for (String temp : map.keySet()) {
			System.out.print(temp + ":" + map.get(temp) + "\t");
		}
		System.out.println();
		// 方式二:通过entrySet()方法
		for (Entry<String, String> entry : map.entrySet()) {
			System.out.print(entry.getKey() + ":" + entry.getValue() + "\t");
		}
		System.out.println();
		// 方式三:jdk8中通过forEach
		map.forEach((key, value) -> {
			System.out.print(key + ":" + value + "\t");
		});
		System.out.println();
		// 方式四:迭代器
		Iterator<String> it = map.keySet().iterator();
		Iterator<String> it1 = map.values().iterator();
		while (it.hasNext()) {
			System.out.print(it.next() + ":" + it1.next() + "\t");
		}

		

27.

Arrays.stream(perms.trim().split(StrUtil.COMMA));

1.使用trim()方法去除字符串perms两端的空白字符,

2.使用split(StrUtil.COMMA)方法将字符串按照逗号分隔,得到一个字符串数组;

3.使用Arrays.stream()方法将字符串数组转换为一个字符串流。

28.TransmittableThreadLocal<>() 是一个 Java 类,用于在多线程环境下传递线程局部变量。它继承自 InheritableThreadLocal<T> 类,并实现了 java.io.Serializable 接口,以便在序列化和反序列化时保持线程局部变量的值

29.<dependencyManagement></dependencyManagement>表示模块依赖,父类模块指定版本后其子类不需要再去指定依赖的版本,他会使用他的父类版本

  • 12
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值