原文链接:https://blog.youkuaiyun.com/KimSoft/article/details/5106984
// 当前时间
Date now = new Date();
// 2010上海世博会开幕时间
Date expo2010BeginDate = DateUtils.parseDate("2010-05-01 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });
// 2010上海世博会闭幕时间
Date expo2010EndDate = DateUtils.parseDate("2010-10-31 23:59:59", new String[] { "yyyy-MM-dd HH:mm:ss" });
System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - now.getTime(), "d"));
System.out.println(DurationFormatUtils.formatDuration(expo2010BeginDate.getTime() - expo2010EndDate.getTime(),
"d"));
Duration formatting utilities and constants. The following table describes the tokens used in the pattern language for formatting.
以下是format参数
character | duration element |
---|---|
y | years |
M | months |
d | days |
H | hours |
m | minutes |
s | seconds |
S | milliseconds |
/**
* 将时间间隔毫秒值转化为**分**秒**毫秒
* @param intervalTime
* @return
*/
public static String convertMillisecondsToMSS(Long intervalTime) {
String str=DurationFormatUtils.formatDuration(intervalTime, "mm分ss秒SSS毫秒");
return str;
}
/**
* 将时间间隔毫秒值转化为**分**秒
* @param intervalTime
* @return
*/
public static String convertMillisecondsToMS(Long intervalTime) {
String str=DurationFormatUtils.formatDuration(intervalTime, "mm分ss秒");
return str;
}
/**
* 将时间间隔毫秒值转化为**时**分
* @param intervalTime
* @return
*/
public static String convertMillisecondsToHM(Long intervalTime) {
String str = DurationFormatUtils.formatDuration(intervalTime, "HH小时mm分");
return str;
}
/**
* 将时间间隔毫秒值转化为**天
* @param intervalTime
* @return
*/
public static String convertMillisecondsToDD(Long intervalTime) {
String str = DurationFormatUtils.formatDuration(intervalTime, "dd天");
return str;
}
/**
* 拿到1天后的时间点
* @param date
* @return
*/
public static Date getNextDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
/**
* 拿到n天以后的时间点
* @param date
* @param number
* @return
*/
public static Date getNextExpiringDate(Date date,int number){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, number);
return calendar.getTime();
}
/**
* 字符串转时间
* @param source
* @return
*/
public static Date StringToDate(String source){
if(null==source || source.equals(""))
return null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 字符串转时间
* @param source
* @param format
* @return
*/
public static Date StringToDate(String source,String format){
if(null==source || source.equals(""))
return null;
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
try {
return dateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 时间转字符串
* @param date
* @return
*/
public static String dateToString(Date date){
if(null==date)
return "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
/**
* 拿到yyyy-MM-dd中的dd
* @param date
* @return
*/
public static int getCurrentDayNumber(Date date){
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DAY_OF_MONTH);
}
总结下Java Calendar的使用:
参考资料:https://www.cnblogs.com/lsy-lancen/p/6122634.html
The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Following are the important points about Calendar
API 文档上给的官方解释:
java.util.Calendar类是一个抽象类,在某一特定的瞬间或日历上,提供年、月、日、小时之间的转换提供方法。
只要来眼界下Calendar中的常量(filed)的作用
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1.运用这些常量赋值,通过这些常量获取值,同样可以通过它进行赋值
2.赋值时,week与mooth是需要注意,week需要制定setFirstDayOfWeek,月份则需要加减1
3.赋值时,一般采用年、月、日、时、分、秒
Calendar.YEAR 、Calendar.MONTH 、Calendar.DAY_OF_MONTH、 Calendar.HOUR_OF_DAY 、Calendar.MINUTE、 Calendar.SECON