Java開發(fā)基礎(chǔ)日期類代碼詳解
由于工作關(guān)系,很久沒更新博客了,今天就給大家?guī)硪黄狫ava實(shí)現(xiàn)獲取指定月份的星期與日期對(duì)應(yīng)關(guān)系的文章,好了,不多說,我們直接上代碼:
一、日期工具類
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;
/**
* 日期工具類,獲取指定月份的星期與日期的對(duì)應(yīng)關(guān)系
* @author liuyazhuang
*
*/
public class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public enum Type{
Year, Month, Date
}
/**
* 獲取兩個(gè)時(shí)間之間的年份
* @param startDate
* @param endDate
* @return
*/
public static int getYears(Date startDate, Date endDate, Type type){
int count = 0;
Calendar calBegin = Calendar.getInstance(); //獲取日歷實(shí)例
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:當(dāng)前月, 1:下一個(gè)月; 2下下月; 以此類推... -1:上一個(gè)月; -2:上上一個(gè)月 ; 以此類推....
* @param length:終止月份, 0:當(dāng)前月, 1:下一個(gè)月; 2下下月; 以此類推... -1:上一個(gè)月; -2:上上一個(gè)月 ; 以此類推....
* @return:日期和星期集合:星期為key 日期為value
*/
public static Map<String, List<String>> getKeyFromMapByValue(int offset, int length){
return getKeyFromMapByValue(getDateKeyWeekValue(offset, length));
}
/**
* 將以date為key, week為value的map轉(zhuǎn)化為以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:當(dāng)前月, 1:下一個(gè)月; 2下下月; 以此類推... -1:上一個(gè)月; -2:上上一個(gè)月 ; 以此類推....
* @param length:終止月份, 0:當(dāng)前月, 1:下一個(gè)月; 2下下月; 以此類推... -1:上一個(gè)月; -2:上上一個(gè)月 ; 以此類推....
* @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;
}
/**
* 獲取當(dāng)前日期所在月份的所有日期,指定月份的所有日期
* @param date:當(dāng)前日期
* @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;
}
/**
* 根據(jù)日期獲得星期
* @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)));
}
}
三、測試結(jié)果
{
"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"
]
}
總結(jié)
本文通過代碼示例向大家展示了日期工具類的幾種用法,希望對(duì)大家學(xué)習(xí)Java有所幫助。
感興趣的朋友可以參閱:Java語言Lang包下常用的工具類介紹、Java AtomicInteger類的使用方法詳解等以及本站其他相關(guān)專題,如有不足之處,歡迎留言指出,小編會(huì)及時(shí)回復(fù)大家并更正。感謝朋友們對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
使用Java注解和反射實(shí)現(xiàn)JSON字段自動(dòng)重命名
這篇文章主要介紹了如何使用Java注解和反射實(shí)現(xiàn)JSON字段自動(dòng)重命名,文中通過代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
springboot接收日期字符串參數(shù)與返回日期字符串類型格式化
這篇文章主要介紹了springboot接收日期字符串參數(shù)與返回日期字符串類型格式化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

