本文作者:kaifamei

Java对日期Date类进行加减运算一二三

更新时间:2023-10-29 09:33:26 人在看 0条评论

Java对日期Date类进行加减运算一二三

2023年10月29日发(作者:2020年五年级班主任工作计划范文)

广东7所高校提前离校-

Java对⽇期Date类进⾏加减运算⼀⼆三⼀、充分利⽤SimpleDateFormatimport DateFormat;import ar;import ;public class DateTestUtil { public static void main(String[] args) throws Exception { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); String str="20110823"; Date dt=(str); Calendar rightNow = tance(); e(dt); (,-1);//⽇期减1年 (,3);//⽇期加3个⽉ (_OF_YEAR,10);//⽇期加10天 Date dt1=e(); String reStr = (dt1); n(reStr); }}注:在Calendar对象的add⽅法中,第⼆个参数为正数表⽰“加”,负数表⽰“减”。⼆、java date ⽇期加减天数import DateFormat;

import ;

public class DateTest {

public static void main(String[] arg){

Date now = new Date();

addAndSubtractDaysByGetTime(now,-5);

addAndSubtractDaysByGetTime(now,5);

addAndSubtractDaysByCalendar(now,-5);

addAndSubtractDaysByCalendar(now,5);

}

public static Date addAndSubtractDaysByGetTime(Date dateTime/*待处理的⽇期*/,int n/*加减天数*/){

//⽇期格式

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");

SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

n((new Date(e() + n * 24 * 60 * 60 * 1000L)));

//n((new Date(e() + n * 24 * 60 * 60 * 1000L)));

//注意这⾥⼀定要转换成Long类型,要不n超过25时会出现范围溢出,从⽽得不到想要的⽇期值

return new Date(e() + n * 24 * 60 * 60 * 1000L);

}

public static Date addAndSubtractDaysByCalendar(Date dateTime/*待处理的⽇期*/,int n/*加减天数*/){ //⽇期格式

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");

SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ar calstart = tance();

e(dateTime);

(_OF_WEEK, n);

n((e()));

//n((e()));

return e();

}

}不推荐这个,代码不够简洁,看着不爽。三、⽹上的别⼈的,太长,没看,粘过来先package ;import DateFormat;import ar;import ;public class DateFormat { /** * ⽇期减⼏年 */ public static String dateMinusYear(String str) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date dt = (str); Calendar rightNow = tance(); e(dt); (, -1);// ⽇期减1年 Date dt1 = e(); String reStr = (dt1); return reStr; } /** * ⽇期加⼏年 */ public static String dateAddYear(String str) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date dt = (str); Calendar rightNow = tance(); e(dt); (, 1);// ⽇期加1年 Date dt1 = e(); String reStr = (dt1); return reStr; } /** * ⽇期减⼏⽉ */ public static String dateMinusMonth(String str) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date dt = (str);//将字符串⽣成Date Calendar rightNow = tance(); e(dt);//使⽤给定的 Date 设置此 Calendar 的时间。

(, -1);// ⽇期减1个⽉ Date dt1 = e();//返回⼀个表⽰此 Calendar 时间值的 Date 对象。 String reStr = (dt1);//将给定的 Date 格式化为⽇期/时间字符串,并将结果添加到给定的 StringBuffer。 return reStr; } /** * ⽇期加⼏⽉ */ public static String dateAddMonth(String str) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); Date dt = (str); Calendar rightNow = tance(); e(dt); (, 1);// ⽇期加3个⽉ // (_OF_YEAR,10);//⽇期加10天 Date dt1 = e(); String reStr = (dt1); return reStr; } /** * 获取当前年⽉的第⼀个⽉的str * @param str * 201505 * @return 201501 * @throws Exception */ public static String dateOneMonth(String str) { str = ing(0, () - 2); str = str + "01"; return str; } /** * 算出所选⽉份距离⼀⽉份有⼏个⽉。 * @param str 201509 * @return 9 */ public static int dateDistanceMonth(String str) { int i = nt(str); int j = nt(eMonth(str)); n(i - j); return i - j + 1; } /** * 获取两个时间的时间差,精确到毫秒 * @param str * @return */ public static String TimeDifference(long start, long end) { long between = end - start; long day = between / (24 * 60 * 60 * 1000); long hour = (between / (60 * 60 * 1000) - day * 24); long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); String timeDifference = day + "天" + hour + "⼩时" + min + "分" + s + "秒" + ms + "毫秒"; return timeDifference; }} /** * 获取24⼩时、⼀周、⼀个⽉的起始时间 *

* @param timeInterval * : DAY_TIME_INTERVAL WEEK_TIME_INTERVAL MONTH_TIME_INTERVAL * @return "yyyy-mm-dd hh:mm:ss" */ public static String getStartTime(int timeInterval) { Calendar cal = tance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (DAY_TIME_INTERVAL == timeInterval) {// 获取24⼩时的起始时间 (_OF_DAY, 0); (, 0); (, 0); String startTime = (e()); return startTime; } else if (WEEK_TIME_INTERVAL == timeInterval) { int weekday = (_OF_WEEK) - 1; (, -weekday); (_OF_DAY, 0); (, 0); (, 0); String startTime = (e()); return startTime; } else if (MONTH_TIME_INTERVAL == timeInterval) { int dayofmonthMin = ualMinimum(_OF_MONTH); // (, -dayofmonth); (, dayofmonthMin); (_OF_DAY, 0); (, 0); (, 0); String startTime = (e()); return startTime; } return null; } /** * 获取24⼩时、⼀周、⼀个⽉的结束时间 *

* @param timeInterval * : DAY_TIME_INTERVAL WEEK_TIME_INTERVAL MONTH_TIME_INTERVAL * @return "yyyy-mm-dd hh:mm:ss" */ public static String getEndTime(int timeInterval) { Calendar cal = tance(); eZone(eZone("GMT+8")); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (DAY_TIME_INTERVAL == timeInterval) { (_OF_DAY, 23); (12, 59); (13, 59); long date = eInMillis(); String endTime = (new Date(date)); return endTime; } else if (WEEK_TIME_INTERVAL == timeInterval) { } else if (WEEK_TIME_INTERVAL == timeInterval) { int weekday = (_OF_WEEK) - 1; (, -weekday); (, 6); (_OF_DAY, 23); (12, 59); (13, 59); long date = eInMillis(); String endTime = (new Date(date)); return endTime; } else if (MONTH_TIME_INTERVAL == timeInterval) { int dayOfMonthMax = ualMaximum(_OF_MONTH); (, dayOfMonthMax); (_OF_DAY, 23); (, 59); (, 59); String endTime = (e()); return endTime; } return null; } /** * 判断dateStr是否在start和end中间,start和end都可以为null yyyyMMddHHmmss或者yyyyMMdd格式 *

* @author * @date 2015年8⽉19⽇下午3:11:46 * @param dateStr * @param start * @param end * @return */ public static boolean checkDateVal(String dateStr, String start, String end) { boolean isDateRight = false; Date date = null; Date startDate = null; Date endDate = null; SimpleDateFormat sdf = null; // 判断⽇期格式 if (14 == ()) { sdf = new SimpleDateFormat("yyyyMMddHHmmss"); } else if (8 == ()) { sdf = new SimpleDateFormat("yyyyMMdd"); } else return false; try { // 更改判断⽇期格式 date = (dateStr); } catch (ParseException e) { (e, e); } if ((start == null) && (end != null)) { try { endDate = (end); } catch (ParseException ex1) { (ex1, ex1); } if ((date != null) && (endDate != null))// Check parameters for { if (eTo(endDate) <= 0) isDateRight = true; } } else if ((start != null) && (end == null)) { try { startDate = (start); } catch (ParseException ex1) { (ex1, ex1); } if ((date != null) && (startDate != null)) { if (eTo(startDate) >= 0) isDateRight = true; } } else if ((start != null) && (end != null)) { try { startDate = (start); endDate = (end); } catch (ParseException ex2) { n(ng()); } if ((startDate != null) && (date != null) && (endDate != null)) { if ((eTo(startDate) >= 0) && (eTo(endDate) <= 0)) isDateRight = true; } } return isDateRight; } /** * 判断dateStr是否在start和end中间,start和end都可以为null long形格式 *

* @author * @date 2015年8⽉19⽇下午3:12:35 * @param dateStr * @param start * @param end * @return */ public static boolean checkDateV(String dateStr, String start, String end) { boolean isDateRight = false; long date = -1; long fromDate = -1; long toDate = -1; date = ong(dateStr); if ((start == null) && (end == null)) { isDateRight = true; } else if ((start == null) && (end != null)) { try { toDate = ong(end); } catch (NumberFormatException nfe) { (nfe, nfe); } if (date <= toDate) { isDateRight = true; } } else if ((start != null) && (end == null)) { try { fromDate = ong(start); } catch (NumberFormatException nfe) { (nfe, nfe); } if (date >= fromDate) { isDateRight = true; } } else if ((start != null) && (end != null)) { try { toDate = ong(end); fromDate = ong(start); } catch (NumberFormatException nfe) { (nfe, nfe); } if ((date <= toDate) && (date >= fromDate)) { isDateRight = true; } } return isDateRight; }四、⾃⼰的⽅法的,简单说下需求,由于数据库⾥的存储类型是 “2017-07-04 12:00:00”,要按当前时间向前检索传进来的天数(noTradeDays),利⽤的GregorianCalendar,结合SimpleDateFormat,完成当前时间逆推的需求

历史人物的故事300字-


文章投稿或转载声明

本文链接:https://www.en369.cn/fanwen/xinxi-2-1191174-0.html

来源:范文频道-369作文网版权所有,转载请保留出处。本站文章发布于 2023-10-29 09:33:26

发表评论

验证码:
用户名: 密码: 匿名发表
评论列表 (有 条评论
2人围观
参与讨论