温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现简单日期计算功能

发布时间:2020-09-01 16:43:19 来源:脚本之家 阅读:116 作者:集成显卡 栏目:编程语言

本文讲的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实现简单日期计算功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI