本文讲的java日期计算比较偏,用到的地方很少(比如获取今天所在周的周一或者周日,获取今天是本月的第几周...),这些方法是以前做项目遗留下来的,现在整理一下,跟大家分享。
工具类主要有一下方法:
public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception
获取指定月份的第一个星期一,比如2014-12 月的第一个周一是2014-12-01
public static int figureweekindexofmonth(string datestring, string dateformat) throws exception
计算指定时间属于月份中的第几周,比如2014-12月的第一周是1号到7号,那么2014-12-05 就是12月的第一周,2014-12-12 就是第二周
public static string getmondyoftoday(string format)
获取今天所在周的星期一, 返回一个时间字符串。 如今天是2014-12-8,那么返回的是: 2014-12-08 (今天刚好是本周周一)
public static date getsundayoftoday()
获取今天所在周的星期天, 如今天是2014-12-8,那么返回的是 2014-12-14
下面是工具类的详细代码:
import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.date; import java.util.list; /** * @文件名称 :dateutil.java * @所在包 :com.nerve.human.common.util * @功能描述 : * 时间格式工具类 * @创建者 :集成显卡 1053214511@qq.com * @公司:ibm gdc * @创建日期 :2013-4-9 * @log : */ public class dateutil { public static date todate(string timestring, string format) throws exception{ return new simpledateformat(format).parse(timestring); } /** * * @method name: tostring * @return type: string * @param date * @param format * @return */ public static string tostring(date date, string format){ string strtime = null; try { simpledateformat simpledateformat = new simpledateformat(format); strtime = simpledateformat.format(date); } catch (exception ex) { system.err.println("格式化日期错误 : " + ex.getmessage()); } return strtime; } /** * 获取当月的第一个星期一(以中国为例) * @method name: getfirstmonday * @return type: void */ public static date getfirstmondayofmonth(string month) throws exception{ return getfirstmondayofmonth(month, "yyyy-mm"); } /** * 获取当月的第一个星期一(以中国为例) * @method name: getfirstmonday * @return type: void */ public static date getfirstmondayofmonth(string datestring, string dateformat) throws exception{ date date = todate(datestring, dateformat); calendar c = calendar.getinstance(); c.settime(date); int step = (9 - c.get(calendar.day_of_week)) % 7; c.add(calendar.day_of_year, step); return c.gettime(); } /** * 计算指定时间属于月份中的第几周 * 比如2014-12月的第一周是1号到7号 * 那么2014-12-05 就是12月的第一周 * 2014-12-12 就是第二周 * * @method name: figureweekindexofmonth * @return type: int * * @param date * @return */ public static int figureweekindexofmonth(string datestring, string dateformat) throws exception{ calendar c = calendar.getinstance(); date curdate = todate(datestring, dateformat); c.settime(curdate); int day = c.get(calendar.day_of_month); simpledateformat sdf = new simpledateformat("yyyy-mm"); date firstmondy = getfirstmondayofmonth(sdf.format(c.gettime())); c.settime(firstmondy); int index = 0; do{ c.add(calendar.day_of_month, 7); index ++; } while(c.get(calendar.day_of_month) < day); return index; } /** * 获取今天所在周的星期一 * @method name: getmondyoftoday * @return type: string * * @return */ public static string getmondyoftoday(string format){ calendar c = calendar.getinstance(); int step = c.get(calendar.day_of_week); //星期天 if(step == 1) step = 6; else step -= 2; c.add(calendar.day_of_year, -step); return tostring(c.gettime(), format); } /** * 获取今天所在周的星期天 * @method name: getmondyoftoday * @return type: string * * @return */ public static date getsundayoftoday(){ calendar c = calendar.getinstance(); int step = c.get(calendar.day_of_week); if(step != calendar.sunday) c.add(calendar.day_of_year, 8-step); return c.gettime(); } /** * 获取指定时间所在的星期天 * @param date * @return */ public static date getsundayofdate(date date){ calendar c = calendar.getinstance(); c.settime(date); int step = c.get(calendar.day_of_week); if(step != calendar.sunday) c.add(calendar.day_of_year, 8-step); return c.gettime(); } }
来个测试截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
网友评论