栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现简单日期计算功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
 

public class DateUtil {
 
 public static Date toDate(String timeString, String format) throws Exception{
 return new SimpleDateFormat(format).parse(timeString);
 }
 
 
 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;
 }
 
 public static Date getFirstMondayOfMonth(String month) throws Exception{
 return getFirstMondayOfMonth(month, "yyyy-MM");
 }
 
 
 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();
 }
 
 
 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;
 }
 
 
 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);
 }
 
 
 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();
 }
 
 
 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();
 }
}

来个测试截图:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号