AppTest.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.java110.user;
  2. import com.java110.utils.util.DateUtil;
  3. import junit.framework.Test;
  4. import junit.framework.TestCase;
  5. import junit.framework.TestSuite;
  6. import java.math.BigDecimal;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. /**
  10. * Unit test for simple App.
  11. */
  12. public class AppTest
  13. extends TestCase
  14. {
  15. /**
  16. * Create the test case
  17. *
  18. * @param testName name of the test case
  19. */
  20. public AppTest( String testName )
  21. {
  22. super( testName );
  23. }
  24. /**
  25. * @return the suite of tests being tested
  26. */
  27. public static Test suite()
  28. {
  29. return new TestSuite( AppTest.class );
  30. }
  31. public void testSplit(){
  32. String a= "1-1-1-1001";
  33. String[] as = a.split("-",3);
  34. for(String aa :as){
  35. System.out.println(aa);
  36. }
  37. }
  38. /**
  39. * Rigourous Test :-)
  40. */
  41. public void testApp()
  42. {
  43. Date startTime = DateUtil.getDateFromStringB("2022-07-01");
  44. Date endTime = DateUtil.getDateFromStringB("2022-07-27");
  45. double month = dayCompare(startTime,endTime);
  46. System.out.println(month);
  47. }
  48. /**
  49. * 计算 两个时间点月份
  50. *
  51. * @param fromDate 开始时间
  52. * @param toDate 结束时间
  53. * @return
  54. */
  55. public double dayCompare(Date fromDate, Date toDate) {
  56. double resMonth = 0.0;
  57. Calendar from = Calendar.getInstance();
  58. from.setTime(fromDate);
  59. Calendar to = Calendar.getInstance();
  60. to.setTime(toDate);
  61. //比较月份差 可能有整数 也会负数
  62. int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
  63. //比较年差
  64. int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
  65. //真实 相差月份
  66. result = result + month;
  67. //开始时间 2021-06-01 2021-08-05 result = 2 2021-08-01
  68. Calendar newFrom = Calendar.getInstance();
  69. newFrom.setTime(fromDate);
  70. newFrom.add(Calendar.MONTH, result);
  71. //如果加月份后 大于了当前时间 默认加 月份 -1 情况 12-19 21-01-10
  72. //这个是神的逻辑一定好好理解
  73. if (newFrom.getTime().getTime() > toDate.getTime()) {
  74. newFrom.setTime(fromDate);
  75. result = result - 1;
  76. newFrom.add(Calendar.MONTH, result);
  77. }
  78. // t1 2021-08-01 t2 2021-08-05
  79. long t1 = newFrom.getTime().getTime();
  80. long t2 = to.getTime().getTime();
  81. //相差毫秒
  82. double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
  83. BigDecimal tmpDays = new BigDecimal(days); //相差天数
  84. BigDecimal monthDay = null;
  85. Calendar newFromMaxDay = Calendar.getInstance();
  86. newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
  87. newFromMaxDay.add(Calendar.MONTH, 1); //下个月1号
  88. //在当前月中 这块有问题
  89. if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
  90. monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  91. return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
  92. }
  93. // 上月天数
  94. days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
  95. tmpDays = new BigDecimal(days);
  96. monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
  97. BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
  98. //下月天数
  99. days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
  100. tmpDays = new BigDecimal(days);
  101. monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
  102. resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
  103. return resMonth;
  104. }
  105. }