当前位置: 移动技术网 > IT编程>开发语言>Java > Java开发基础日期类代码详解

Java开发基础日期类代码详解

2019年07月19日  | 移动技术网IT编程  | 我要评论

由于工作关系,很久没更新博客了,今天就给大家带来一篇java实现获取指定月份的星期与日期对应关系的文章,好了,不多说,我们直接上代码:

一、日期工具类

package com.lyz.date;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.calendar;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import com.chwl.medical.utils.common.collectionutils;
import com.chwl.medical.utils.common.objectutils;
/**
 * 日期工具类,获取指定月份的星期与日期的对应关系
 * @author liuyazhuang
 *
 */
public class dateutils {
	public static final string date_format = "yyyy-mm-dd";
	public enum type{
		year, month, date
	}
	/**
	 * 获取两个时间之间的年份
	 * @param startdate
	 * @param enddate
	 * @return
	 */
	public static int getyears(date startdate, date enddate, type type){
		int count = 0;
		 calendar calbegin = calendar.getinstance(); //获取日历实例 
		 calendar calend = calendar.getinstance(); 
		 calbegin.settime(startdate);
		 calend.settime(enddate);
		 if(type.year == type){
			 count = calend.get(calendar.year) - calbegin.get(calendar.year); 
		 }else if(type.month == type){
			 count = calend.get(calendar.month) - calbegin.get(calendar.month); 
		 }else{
			 count = calend.get(calendar.date) - calbegin.get(calendar.date); 
		 }
		 return count;
	}
	/**
	 * 获取指定月份的所有日期和星期集合
	 * @param offset:起止月份, 0:当前月, 1:下一个月; 2下下月; 以此类推... -1:上一个月; -2:上上一个月 ; 以此类推....
	 * @param length:终止月份, 0:当前月, 1:下一个月; 2下下月; 以此类推... -1:上一个月; -2:上上一个月 ; 以此类推....
	 * @return:日期和星期集合:星期为key 日期为value
	 */
	public static map<string, list<string>> getkeyfrommapbyvalue(int offset, int length){
		return getkeyfrommapbyvalue(getdatekeyweekvalue(offset, length));
	}
	/**
	 * 将以date为key, week为value的map转化为以week为key, date为value的map
	 * @param dateweek
	 * @return
	 */
	public static map<string, list<string>> getkeyfrommapbyvalue(map<string, string> dateweek){
		map<string, list<string>> weekdate = new hashmap<string, list<string>>();
		if(!collectionutils.isempty(dateweek)){
			for(map.entry<string, string> entry : dateweek.entryset()){
				//获取日期集合
				list<string> list = weekdate.get(entry.getvalue());
				if(objectutils.isempty(list)){
					list = new arraylist<string>();
				}
				list.add(entry.getkey());
				weekdate.put(entry.getvalue(), list);
			}
		}
		return weekdate;
	}
	/**
	 * 获取指定月份的所有日期和星期集合
	 * @param offset:起止月份, 0:当前月, 1:下一个月; 2下下月; 以此类推... -1:上一个月; -2:上上一个月 ; 以此类推....
	 * @param length:终止月份, 0:当前月, 1:下一个月; 2下下月; 以此类推... -1:上一个月; -2:上上一个月 ; 以此类推....
	 * @return:日期和星期集合:日期为key 星期为value
	 */
	public static map<string, string> getdatekeyweekvalue(int offset, int length){
		map<string, string> map = new hashmap<string, string>();
		for(int i = offset; i <= length; i++){
			list<date> list = getallthedateofthemonth(new date(),i);
			for(date date: list){
				string weekday = getdateofweek(date);
				map.put(parsedatetostring(date, date_format), weekday);
			}
		}
		return map;
	}
	/**
	 * 获取当前日期所在月份的所有日期,指定月份的所有日期
	 * @param date:当前日期
	 * @param n:1下一月;2:下下月..以此类推; -1:上月,-2:上上月...以此类推
	 * @return:返回指定月份的所有日期
	 */
	public static list<date> getallthedateofthemonth(date date, int n) {
		list<date> list = new arraylist<date>();
		calendar cal = calendar.getinstance();
		cal.settime(date);
		cal.set(calendar.date, 1);
		cal.add(calendar.month, n);
		int month = cal.get(calendar.month);
		while(cal.get(calendar.month) == month){
			list.add(cal.gettime());
			cal.add(calendar.date, 1);
		}
		return list;
	}
	/**
	 * 根据日期获得星期
	 * @param date
	 * @return
	 */
	public static string getdateofweek(date date) {
		//string[] weekdaysname = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		string[] weekdayscode = { "0", "1", "2", "3", "4", "5", "6" };
		calendar calendar = calendar.getinstance();
		calendar.settime(date);
		int intweek = calendar.get(calendar.day_of_week) - 1;
		if(intweek < 0) intweek = 0;
		return weekdayscode[intweek];
	}
	public static string parsedatetostring(date date, string formatstring) {
		return getsimpledateformat(formatstring).format(date);
	}
	public static simpledateformat getsimpledateformat(string formatstring) {
		return new simpledateformat(formatstring);
	}
}

二、测试类

package com.lyz.date; 
import net.sf.json.jsonobject; 
/** 
 * 测试工具类 
 * @author liuyazhuang 
 * 
 */ 
public class testdate { 
  public static void main(string[] args) { 
    system.out.println(jsonobject.fromobject(dateutils.getdatekeyweekvalue(-1, 1))); 
    system.out.println(jsonobject.fromobject(dateutils.getkeyfrommapbyvalue(-1,1))); 
  } 
} 

三、测试结果

{
  "2017-02-28": "2",
  "2017-04-19": "3",
  "2017-04-17": "1",
  "2017-02-25": "6",
  "2017-04-18": "2",
  "2017-02-24": "5",
  "2017-04-15": "6",
  "2017-02-27": "1",
  "2017-04-16": "0",
  "2017-02-26": "0",
  "2017-04-13": "4",
  "2017-02-21": "2",
  "2017-04-14": "5",
  "2017-02-20": "1",
  "2017-04-11": "2",
  "2017-02-23": "4",
  "2017-04-12": "3",
  "2017-02-22": "3",
  "2017-04-21": "5",
  "2017-04-20": "4",
  "2017-04-08": "6",
  "2017-04-09": "0",
  "2017-04-04": "2",
  "2017-04-05": "3",
  "2017-04-06": "4",
  "2017-04-07": "5",
  "2017-04-01": "6",
  "2017-04-02": "0",
  "2017-04-03": "1",
  "2017-04-10": "1",
  "2017-02-07": "2",
  "2017-02-06": "1",
  "2017-02-09": "4",
  "2017-02-08": "3",
  "2017-03-29": "3",
  "2017-03-25": "6",
  "2017-03-26": "0",
  "2017-03-27": "1",
  "2017-02-01": "3",
  "2017-03-28": "2",
  "2017-03-21": "2",
  "2017-02-03": "5",
  "2017-03-22": "3",
  "2017-02-02": "4",
  "2017-03-23": "4",
  "2017-02-05": "0",
  "2017-03-24": "5",
  "2017-02-04": "6",
  "2017-03-31": "5",
  "2017-03-30": "4",
  "2017-04-23": "0",
  "2017-04-22": "6",
  "2017-02-19": "0",
  "2017-04-25": "2",
  "2017-02-18": "6",
  "2017-04-24": "1",
  "2017-02-17": "5",
  "2017-04-27": "4",
  "2017-04-26": "3",
  "2017-04-29": "6",
  "2017-03-18": "6",
  "2017-04-28": "5",
  "2017-03-19": "0",
  "2017-02-12": "0",
  "2017-03-16": "4",
  "2017-02-11": "6",
  "2017-03-17": "5",
  "2017-02-10": "5",
  "2017-03-14": "2",
  "2017-03-15": "3",
  "2017-02-16": "4",
  "2017-03-12": "0",
  "2017-02-15": "3",
  "2017-03-13": "1",
  "2017-02-14": "2",
  "2017-03-10": "5",
  "2017-02-13": "1",
  "2017-03-11": "6",
  "2017-03-20": "1",
  "2017-03-09": "4",
  "2017-03-08": "3",
  "2017-03-07": "2",
  "2017-03-06": "1",
  "2017-03-05": "0",
  "2017-03-04": "6",
  "2017-03-03": "5",
  "2017-03-02": "4",
  "2017-04-30": "0",
  "2017-03-01": "3"
}
{
  "3": [
    "2017-04-19",
    "2017-04-12",
    "2017-02-22",
    "2017-04-05",
    "2017-02-08",
    "2017-03-29",
    "2017-02-01",
    "2017-03-22",
    "2017-04-26",
    "2017-03-15",
    "2017-02-15",
    "2017-03-08",
    "2017-03-01"
  ],
  "2": [
    "2017-02-28",
    "2017-04-18",
    "2017-02-21",
    "2017-04-11",
    "2017-04-04",
    "2017-02-07",
    "2017-03-28",
    "2017-03-21",
    "2017-04-25",
    "2017-03-14",
    "2017-02-14",
    "2017-03-07"
  ],
  "1": [
    "2017-04-17",
    "2017-02-27",
    "2017-02-20",
    "2017-04-03",
    "2017-04-10",
    "2017-02-06",
    "2017-03-27",
    "2017-04-24",
    "2017-03-13",
    "2017-02-13",
    "2017-03-20",
    "2017-03-06"
  ],
  "0": [
    "2017-04-16",
    "2017-02-26",
    "2017-04-09",
    "2017-04-02",
    "2017-03-26",
    "2017-02-05",
    "2017-04-23",
    "2017-02-19",
    "2017-03-19",
    "2017-02-12",
    "2017-03-12",
    "2017-03-05",
    "2017-04-30"
  ],
  "6": [
    "2017-02-25",
    "2017-04-15",
    "2017-04-08",
    "2017-04-01",
    "2017-03-25",
    "2017-02-04",
    "2017-04-22",
    "2017-02-18",
    "2017-04-29",
    "2017-03-18",
    "2017-02-11",
    "2017-03-11",
    "2017-03-04"
  ],
  "5": [
    "2017-02-24",
    "2017-04-14",
    "2017-04-21",
    "2017-04-07",
    "2017-02-03",
    "2017-03-24",
    "2017-03-31",
    "2017-02-17",
    "2017-04-28",
    "2017-03-17",
    "2017-02-10",
    "2017-03-10",
    "2017-03-03"
  ],
  "4": [
    "2017-04-13",
    "2017-02-23",
    "2017-04-20",
    "2017-04-06",
    "2017-02-09",
    "2017-02-02",
    "2017-03-23",
    "2017-03-30",
    "2017-04-27",
    "2017-03-16",
    "2017-02-16",
    "2017-03-09",
    "2017-03-02"
  ]
}

总结

本文通过代码示例向大家展示了日期工具类的几种用法,希望对大家学习java有所帮助。

感兴趣的朋友可以参阅:java语言lang包下常用的工具类介绍java atomicinteger类的使用方法详解等以及本站其他相关专题,如有不足之处,欢迎留言指出,小编会及时回复大家并更正。感谢朋友们对移动技术网网站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网