• java 获取两个日期之间的所有月份 (年月)、以及月数、年数


    1. /**
    2. * 获取两个日期之间的所有月份 (年月)
    3. *
    4. * @param startTime
    5. * @param endTime
    6. * @return:YYYY-MM
    7. */
    8. public static List<String> getMonthBetweenDate(String startTime, String endTime){
    9. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    10. // 声明保存日期集合
    11. List<String> list = new ArrayList<String>();
    12. try {
    13. // 转化成日期类型
    14. Date startDate = sdf.parse(startTime);
    15. Date endDate = sdf.parse(endTime);
    16. //用Calendar 进行日期比较判断
    17. Calendar calendar = Calendar.getInstance();
    18. while (startDate.getTime()<=endDate.getTime()){
    19. // 把日期添加到集合
    20. list.add(sdf.format(startDate));
    21. // 设置日期
    22. calendar.setTime(startDate);
    23. //把日期增加一天
    24. calendar.add(Calendar.MONTH, 1);
    25. // 获取增加后的日期
    26. startDate=calendar.getTime();
    27. }
    28. } catch (ParseException e) {
    29. e.printStackTrace();
    30. }
    31. return list;
    32. }
    33. public static void main(String[] args) throws ParseException {
    34. String startStr = "2021-02-26";
    35. String endStr = "2022-03-09";
    36. List<String> list = getMonthBetweenDate(startStr, endStr);
    37. System.out.println(list);
    38. }

    输出结果

    1. /**
    2. * 根据两个时间获取相差的月数
    3. * @param d1 日期起期
    4. * @param d2 日期止期
    5. * @return
    6. */
    7. public static int getMonth(Date d1, Date d2) {
    8. Calendar c1 = Calendar.getInstance();
    9. Calendar c2 = Calendar.getInstance();
    10. c1.setTime(d1);
    11. c2.setTime(d2);
    12. int year1 = c1.get(Calendar.YEAR);
    13. int year2 = c2.get(Calendar.YEAR);
    14. int month1 = c1.get(Calendar.MONTH);
    15. int month2 = c2.get(Calendar.MONTH);
    16. int day1 = c1.get(Calendar.DAY_OF_MONTH);
    17. int day2 = c2.get(Calendar.DAY_OF_MONTH);
    18. // 获取年的差值
    19. int yearInterval = year1 - year2;
    20. // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
    21. if (month1 < month2 || month1 == month2 && day1 < day2) {
    22. yearInterval--;
    23. }
    24. // 获取月数差值
    25. int monthInterval = (month1 + 12) - month2;
    26. if (day1 < day2) {
    27. monthInterval--;
    28. }
    29. monthInterval %= 12;
    30. int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
    31. return monthsDiff;
    32. }
    1. /**
    2. * 根据两个日期获取相差年数
    3. * @param startDate 日期起期
    4. * @param endDate 日期止期
    5. * @return
    6. */
    7. public static int getYear(Date startDate, Date endDate) {
    8. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    9. String startDateStr = simpleDateFormat.format(startDate);
    10. String endDateStr = simpleDateFormat.format(endDate);
    11. return (Integer.parseInt(endDateStr) - Integer.parseInt(startDateStr)) / 10000;
    12. }

    根据出生日期算年龄

    1. /**
    2. * 根据出生日期获取当前时间的年龄
    3. * @param birthdate
    4. * @return
    5. */
    6. public static int getYear(Date birthdate) {
    7. Date now = new Date();
    8. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    9. String format1 = simpleDateFormat.format(birthdate);
    10. String format = simpleDateFormat.format(now);
    11. return (Integer.parseInt(format) - Integer.parseInt(format1)) / 10000;
    12. }
    1. package com.xxx;
    2. import org.junit.Test;
    3. import java.text.DecimalFormat;
    4. import java.text.ParseException;
    5. import java.text.SimpleDateFormat;
    6. import java.util.ArrayList;
    7. import java.util.Calendar;
    8. import java.util.Date;
    9. import java.util.List;
    10. /**
    11. * @author Chang
    12. * @program: bmps-cloud-core-prd07
    13. * @description: 测试类
    14. * @date 2022-07-17 15:21:01
    15. */
    16. public class Test02 {
    17. /**
    18. * 截取字符串
    19. */
    20. @Test
    21. public void test1(){
    22. String polApplyDateStr = "20220102";
    23. String insuredBirthdayStr = "20210909";
    24. String substring1 = polApplyDateStr.substring(4, 8);
    25. String substring2 = insuredBirthdayStr.substring(4, 8);
    26. String substring = polApplyDateStr.substring(0, 4);
    27. System.out.println(substring);
    28. System.out.println(substring1);
    29. System.out.println(substring2);
    30. }
    31. /**
    32. * 冒号分割字符串
    33. */
    34. @Test
    35. public void test2(){
    36. String riskAmntHashKey="1:2:3";
    37. String[] split = riskAmntHashKey.split(":");
    38. String s = split[1];
    39. System.out.println(s);
    40. }
    41. /**
    42. *保留两位小数
    43. */
    44. @Test
    45. public void test3(){
    46. double f = 111231.5545;
    47. DecimalFormat df = new DecimalFormat("#.00");
    48. System.out.println(df.format(f));
    49. }
    50. @Test
    51. public void test4() throws ParseException {
    52. /**
    53. * 获取两个日期之间的所有月份 (年月)
    54. *
    55. * @param startTime
    56. * @param endTime
    57. * @return:YYYY-MM
    58. */
    59. String startStr = "2021-07-10";
    60. String endStr = "2022-08-10";
    61. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    62. // 声明保存日期集合
    63. List list = new ArrayList();
    64. // 转化成日期类型
    65. Date startDate = sdf.parse(startStr);
    66. Date endDate = sdf.parse(endStr);
    67. //用Calendar 进行日期比较判断
    68. Calendar calendar = Calendar.getInstance();
    69. while (startDate.getTime() <= endDate.getTime()) {
    70. // 把日期添加到集合
    71. list.add(sdf.format(startDate));
    72. // 设置日期
    73. calendar.setTime(startDate);
    74. //把日期增加一天
    75. calendar.add(Calendar.MONTH, 1);
    76. // 获取增加后的日期
    77. startDate = calendar.getTime();
    78. }
    79. System.out.println(list);
    80. }
    81. }

  • 相关阅读:
    iPhone - 如何找到最顶层的视图控制器
    Windows Server 2019 搭建WEB环境(IIS+CA)
    XmlSerializer 序列化解决内存增加问题
    微信小程序 BLE 多连问题。
    MySQL基础|在Navicat中输入SQL语句步骤【含SQL语句约束规范】
    第二十七章 Classes - 引用其他类成员
    Day36力扣打卡
    adb指令整理
    golang基础 :waitgroup用法及使用要点
    Vue3---uni-app--高德地图引用BUG
  • 原文地址:https://blog.csdn.net/m0_61466807/article/details/125892169