MyUtil.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.chuanghai.smartschool.tuitionpayment.utils;
  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.format.DateTimeFormatter;
  5. /**
  6. * @Author: codingliang
  7. * @Description: 自定义工具类
  8. * @Date: 2021-08-20 10:36
  9. * @Version: V1.0
  10. **/
  11. public class MyUtil {
  12. /**
  13. * 计算学生年级
  14. *
  15. * @param grade 入学年份
  16. * @return 年级,如当前是2021年,学生入学年份是2021,则返回 1
  17. */
  18. public static Integer calcStudentGrade(Integer grade) {
  19. LocalDate now = LocalDate.now();
  20. int currentYear = now.getYear();
  21. int currentMonth = now.getMonthValue();
  22. int currentGrade = currentYear - grade;
  23. if (currentGrade < 0) {
  24. return 1;
  25. } else {
  26. if (currentMonth < 7) {
  27. return currentYear - grade;
  28. } else {
  29. return currentYear - grade + 1;
  30. }
  31. }
  32. }
  33. /**
  34. * 将建行拼接的时间转换成localDateTime时间
  35. * @param orderDate
  36. * @return
  37. */
  38. public LocalDateTime changeTime(String orderDate) {
  39. StringBuilder sb = new StringBuilder(orderDate);//构造一个StringBuilder对象
  40. sb.insert(4, "-");//在指定的位置1,插入指定的字符串
  41. sb.insert(7, "-");//在指定的位置1,插入指定的字符串
  42. sb.insert(10, " ");//在指定的位置1,插入指定的字符串
  43. sb.insert(13, ":");//在指定的位置1,插入指定的字符串
  44. sb.insert(16, ":");//在指定的位置1,插入指定的字符串
  45. orderDate = sb.toString();
  46. LocalDateTime dateTime = LocalDateTime.parse(orderDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  47. return dateTime;
  48. }
  49. public static void main(String[] args) {
  50. LocalDateTime localDateTime = new MyUtil().changeTime("20220708091308");
  51. System.out.println(localDateTime);
  52. System.out.println(calcStudentGrade(2022));
  53. }
  54. }