import java.sql.Timestamp;
import java.text.ParseException;
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;
public class DateUtil {
private DateUtil(){}
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat datetimefomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
private static Map<String, Integer> dayOfWeeks = new HashMap<String, Integer>();
private static List<String> dayOfWeekList = new ArrayList<String>();
static{
dayOfWeeks.put("一", Calendar.MONDAY);
dayOfWeeks.put("二", Calendar.TUESDAY);
dayOfWeeks.put("三", Calendar.WEDNESDAY);
dayOfWeeks.put("四", Calendar.THURSDAY);
dayOfWeeks.put("五", Calendar.FRIDAY);
dayOfWeeks.put("六", Calendar.SATURDAY);
dayOfWeeks.put("日", Calendar.SUNDAY);
dayOfWeekList.add("一");
dayOfWeekList.add("二");
dayOfWeekList.add("三");
dayOfWeekList.add("四");
dayOfWeekList.add("五");
dayOfWeekList.add("六");
dayOfWeekList.add("日");
}
/**
* 获取指定日期所的下一周的星期一的日期,格式为yyyy-MM-dd
* @param date
* @return
*/
public static String getNextMondayDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 7);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return sdf.format(calendar.getTime());
}
/**
* 获取指定日期的下一周的星期日的日期,格式为yyyy-MM-dd
* @param date
* @return
*/
public static String getNextSundayDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 7);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return sdf.format(calendar.getTime());
}
/**
* 计算两个日期之间的分钟数
* @param beginDateTime
* @param endDateTime
* @return
*/
public static Long calculateMinutesBetweenDates(Timestamp beginDateTime, Timestamp endDateTime){
long minutes = (Long.valueOf(endDateTime.getTime() - beginDateTime.getTime()) / 1000 / 60);
return minutes;
}
/**
* 获取指定日期所在星期的星期一的日期,格式为yyyy-MM-dd
* @param date
* @return
*/
public static String getMondayDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return sdf.format(calendar.getTime());
}
/**
* 获取指定日期所在星期的星期四的日期,格式为yyyy-MM-dd
* @param date
* @return
*/
public static String getThursdayDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
return sdf.format(calendar.getTime());
}
/**
* 获取指定日期所在星期的星期日的日期,格式为yyyy-MM-dd
* @param date
* @return
*/
public static String getSundayDate(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return sdf.format(calendar.getTime());
}
/**
* 根据星期获取当前周的日期,格式为yyyy-MM-dd
* @param dayOfWeek 星期,例如:一、二、三、四、五、六和日
* @return
*/
public static String getDateInCurrentWeek(String dayOfWeek){
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeeks.get(dayOfWeek));
return sdf.format(calendar.getTime());
}
/**
* 根据一个日期格式的字符串返回一个Date对象
* @param date 格式:yyyy-MM-dd HH:mm:ss,如果不是该格式,返回当前时间
* @return
*/
public static Date parseDate(String date){
Date result = null;
if(date == null)
result = new Date();
try {
result = datetimefomat.parse(date);
} catch (ParseException e1) {
result = new Date();
System.err.println("occured a error when parse a date, param is: "+date);
}
return result;
}
/**
* 获取当前时间,格式:HH:mm:ss
* @return
*/
public static String getCurrentTime(){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
return timeFormat.format(calendar.getTime());
}
/**
* 获取指定日期对象所在的年
* @param date
* @return
*/
public static int getYear(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
/**
* 获取指定日期对象所在的月
* @param date
* @return
*/
public static int getMonth(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return (calendar.get(Calendar.MONTH)+1);
}
/**
* 获取指定日期对象对应的那一天
* @param date
* @return
*/
public static int getDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DATE);
}
/**
* 获取中文格式的星期,例如:一、二、三,等等
* @param date
* @return
*/
public static String getCNDayOfWeek(Date date){
String result = "";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
for(Map.Entry<String, Integer> entry : dayOfWeeks.entrySet()){
if(entry.getValue() == dayOfWeek){
result = entry.getKey();
break;
}
}
return result;
}
/**
* 获取指定日期在当前年中的星期数,就是说指定日期在当前年中是第几周
* @param date 如果date为空,返回0
* @return
*/
public static int getWeekOfYear(Date date){
if(date == null)
return 0;
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
}
/**
* 返回中文格式的星期列表
* @return 一、二、三、四、五、六和日
*/
public static List<String> getCNDayofWeekList(){
return dayOfWeekList;
}
/**
* 判断指定的日期是否是星期日
* @param date
* @return
*/
public static boolean isSunday(Date date){
if(date == null)
return false;
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK));
}
/**
* 判断指定的日期是否是跨年周
* @param date
* @return
*/
public static boolean isSpanYearsWeek(Date date){
if(date == null)
return false;
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int yearForMonday = calendar.get(Calendar.YEAR);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
int yearForSunday = calendar.get(Calendar.YEAR);
return (yearForMonday != yearForSunday);
}
/**
* 获取指定日期所在的年的最大周数
* @param date
* @return
*/
public static int getMaxWeekOfYear(Date date){
if(date == null)
return 0;
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.WEEK_OF_YEAR);
}
}
分享到:
相关推荐
Java日期工具类 1、日期的各种格式化 2、获取当前年度 3、获取当前月份 4、获取当前天号数 5、获取当前时间往前推一个月的时间 6、获取上个月("MMM yyyy") 7、获取上个月("yyyymm") 8、获取上个月("yyyy-mm") 9、...
java日期工具类,java日期工具类,java日期工具类,java日期工具类
DateUtils(日期工具类),包含日期格式化,解析等。
日期工具类
根据提供的文件信息,本文将对日期工具类进行深入解析,并详细介绍其包含的主要功能与实现方法。此工具类主要涉及到了日期格式化、日期解析、获取当前时间等实用操作。 ### 一、概述 该日期工具类名为 `DateUtil`...
强大的好用的原创日期工具类: 1.返回当前日期字符串 yyyy-MM-dd 2.返回当前日期字符串 yyyy-MM-dd HH:mm:ss 3.根据指定时间格式解析日期字符串为Date对象 4.根据默认时间格式解析日期字符串为Date对象 5.根据指定...
Java日期工具类是Java开发中不可或缺的一部分,它们用于处理日期和时间相关的操作。在Java中,日期和时间处理涉及到多个类和接口,如`java.util.Date`、`java.util.Calendar`、`java.text.SimpleDateFormat`以及Java...
本篇将围绕Java中的日期工具类和时间工具类展开讨论,同时会涉及到日期时间的格式化。 首先,Java 8之前,我们主要依赖`java.util.Date`和`java.text.SimpleDateFormat`这两个类来处理日期和时间。`Date`类用于表示...
各种你能想到的日期计算,操作。包含常见的年月日间隔计算、比较、格式化
`时间日期工具类`是提升开发效率的关键,它们提供了对日期和时间进行格式化、比较、计算等操作的便捷方法。在这个"时间日期工具类(包含java8新特性).zip"压缩包中,我们有两个文件:DateUtil.java和DateUtils.java...