package com.txooo.utils; import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by 徐嘉健 on 2018/10/16. */ public class DateYearUtils { /** * 计算相差多少年 * * @param day * @param dayAddNum * @return */ public static Long dataYear(long currentYear, long bath) { long strTime = 0; strTime = currentYear + bath; return strTime; } /** * 计算相差多少月 * * @param day * @param dayAddNum * @return */ public static String dearMonth(long currentYear, long currentMonth, long bath) { long strmonth = 0; //月 if ((currentMonth + bath) > 12) { strmonth = ((currentMonth + bath) % 12); } else { strmonth = currentMonth + bath; } //年 currentYear = (currentMonth + bath) / 12 + currentYear; return currentYear + "-" + strmonth; } /** * 计算相差多少天 * * @param day * @param dayAddNum * @return */ public static String getDateStr(String day, long dayAddNum) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date nowDate = null; try { nowDate = df.parse(day); } catch (ParseException e) { e.printStackTrace(); } Date newDate2 = new Date(nowDate.getTime() + dayAddNum * 24 * 60 * 60 * 1000); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateOk = simpleDateFormat.format(newDate2); return dateOk; } /** * 计算年的差 * * @param currentYear * @param bath * @return */ public static Long LastYear(long currentYear, long bath) { long strTime = 0; strTime = currentYear - bath; return strTime; } /*** * 计算月与过期时间的差 * @param datetime * @param bath * @return */ public static String dateFormat(String datetime, long bath) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Date date = null; try { date = sdf.parse(datetime); } catch (ParseException e) { e.printStackTrace(); } Calendar cl = Calendar.getInstance(); cl.setTime(date); cl.add(Calendar.MONTH, (int) -bath); date = cl.getTime(); return sdf.format(date); } /** * 日期 天数的 差 * * @param day * @param Num * @return */ public static String getLastDateStr(String day, int Num) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date nowDate = null; try { nowDate = df.parse(day); } catch (ParseException e) { e.printStackTrace(); } //如果需要向后计算日期 -改为+ Date newDate2 = new Date(nowDate.getTime() - (long) Num * 24 * 60 * 60 * 1000); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateOk = simpleDateFormat.format(newDate2); return dateOk; } }
日期算法 年月日进行加减 Utils
最新推荐文章于 2021-05-27 07:20:34 发布