欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java实现简单日期计算功能

时间:2021-08-22 09:49:07|栏目:JAVA代码|点击:

本文讲的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();
 }
}

来个测试截图:

上一篇:Java学习笔记之异常处理

栏    目:JAVA代码

下一篇:使用AOP的@Around后无返回值的解决

本文标题:java实现简单日期计算功能

本文地址:http://www.codeinn.net/misctech/168939.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有