相关说明程序中都有注释.^+^
欢迎多提宝贵意见
一共2个文件:
DateFormat.java --- 时间以及时间格式相关的处理功能
DateFormatTest.java --- 测试文件
主要内容:
时间以及时间格式相关的处理功能,主要是字符串与时间之间的相互转换。
主要功能:
1. long getCurrentTimeMillis()
获得系统的当前时间,毫秒.
例如: 1160655659140
2. Date getCurrentDate()
获得系统的当前时间
例如:Thu Oct 12 20:20:59 CST 2006
3. String getCurrentFormatDate()
获得系统当前日期,以默认格式显示
例如:2006-10-12
4. String getCurrentFormatDateTime()
获得系统当前日期时间,以默认格式显示
例如:2006-10-12 20:20:59
5. String getCurrentCustomFormatDateTime(String pattern)
获得系统当前日期时间,按照指定格式返回
例如:par:pattern = "yyyy-MM-dd HH:mm"
res:2006-10-12 20:20
6. String formatDate(Date date, String pattern)
输入日期,按照指定格式返回
例如:par:pattern = "yyMM"
res: 0610
7. String formatDate(Date date, String pattern, Locale locale)
输入日期,按照指定格式返回
例如:par:pattern = "yyyyMMdd"
locale = new Locale("zh","CN","") /*中国地区*/
res: 20061012
8. Date parseStrToDate(String dateStr)
将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Date
9. Date parseStrToDateTime(String dateStr)
将时间字符串按照默认格式DATE_TIME_FORMAT ="yyyy-MM-dd HH:mm:ss",转换为Date
10. Calendar parseStrToCalendar(String dateStr)
将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Calender
11. String parseDateStrToDateTimeStr(String dateStr)
将日期字符串转换成日期时间字符串
12. Date parseStrToCustomPatternDate(String dateStr, String pattern)
将时间或者时间日期字符串按照指定格式转换为Date
13. String convertDatePattern(String dateStr, String patternFrom, String patternTo)
将时间字符串从一种格式转换成另一种格式
14. Date addDays(Date date, int days)
日期天数增加
15. Date minusDays(Date date, int days)
日期天数减少
16. String addDate(String dateStr, String pattern, String type, int count)
按时间格式相加
17. String minusDate(String dateStr, String pattern, String type, int count)
按时间格式相减
18. int compareDate(String dateStr1, String dateStr2, String pattern)
日期大小比较
19. String getFirstDayInMonth(String dateStr)
获得这个月的第一天
20. String getLastDayInMonth(String dateStr)
获得这个月的最后一天
21. String getFirstDayInWeek(String dateStr)
获得这周的第一天
22. String getLastDayInWeek(String dateStr)
获得这周的最后一天
文件1:DateFormat.java
package Common.Utils.DateUtils;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
*
* <p>Description: 时间以及时间格式相关的处理功能</p>
* <p>Copyright 2006.</p>
* @author ma jia nan
* @Create Date : 2006-10-12
* @Version : 1.0
*/
public class DateFormat {
// 系统默认日期格式
public static final String DATE_FORMAT = " yyyy-MM-dd " ;
// 系统默认日起时间格式
public static final String DATE_TIME_FORMAT = " yyyy-MM-dd HH:mm:ss " ;
// 8位日期格式
public static final String DATE_FORMAT_8 = " yyyyMMdd " ;
// 14为日期时间格式
public static final String DATE_TIME_FORMAT_14 = " yyyyMMddHHmmss " ;
public final static String YEAR = " year " ;
public final static String MONTH = " month " ;
public final static String DAY = " day " ;
public final static String WEEK = " week " ;
public final static String HOUR = " hour " ;
public final static String MINUTE = " minute " ;
public final static String SECOND = " second " ;
/**
* 判断参数是否等于null或者空
* @param para
* @return
*/
private static boolean checkPara(Object para) {
if ( null == para || "" .equals(para)) {
return true ;
} else {
return false ;
}
}
/**
* 获得系统的当前时间,毫秒.
* @return
*/
public static long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 获得系统的当前时间
* @return
* e.g.Thu Oct 12 10:25:14 CST 2006
*/
public static Date getCurrentDate() {
// return new Date(System.currentTimeMillis());
return new Date(getCurrentTimeMillis());
}
/**
* 获得系统当前日期,以默认格式显示
* @return
* e.g.2006-10-12
*/
public static String getCurrentFormatDate() {
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
return dateFormator.format(currentDate);
}
/**
* 获得系统当前日期时间,以默认格式显示
* @return
* e.g.2006-10-12 10:55:06
*/
public static String getCurrentFormatDateTime() {
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
return dateFormator.format(currentDate);
}
/**
* 获得系统当前日期时间,按照指定格式返回
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者介于二者之间的格式,e.g."yyyyMMddHH"
* @return
* e.g.200610121115
*/
public static String getCurrentCustomFormatDateTime(String pattern) {
if (checkPara(pattern)) {
return "" ;
}
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern);
return dateFormator.format(currentDate);
}
/**
* 输入日期,按照指定格式返回
* @param date
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者类似于二者的格式,e.g."yyyyMMddHH","yyyyMM"
* @return
*/
public static String formatDate(Date date, String pattern) {
if (checkPara(pattern) || checkPara(date)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern);
return dateFormator.format(date);
}
/**
* 输入日期,按照指定格式返回
* @param date
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者类似于二者的格式,e.g."yyyyMMddHH","yyyyMM"
* @param loc 国家地区,
* e.g.new Locale("zh","CN","") 中国
* Locale.getDefault();
* @return
*/
public static String formatDate(Date date, String pattern, Locale locale) {
if (checkPara(pattern) || checkPara(date)) {
return "" ;
}
if (checkPara(locale)) {
locale = Locale.getDefault();
}
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern, locale);
String customFormatDate = dateFormator.format(date);
return customFormatDate;
}
/**
* 将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Date
* @param dateStr
* @return
*/
public static Date parseStrToDate(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串按照默认格式DATE_TIME_FORMAT ="yyyy-MM-dd HH:mm:ss",转换为Date
* @param dateStr
* @return
*/
public static Date parseStrToDateTime(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Calender
* @param dateStr
* @return
*/
public static Calendar parseStrToCalendar(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
Locale locale = Locale.getDefault();
Calendar cal = new GregorianCalendar(locale);
cal.setTime(resDate);
return cal;
}
/**
* 将日期字符串转换成日期时间字符串
* @param dateStr
* @return
*/
public static String parseDateStrToDateTimeStr(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return formatDate(resDate,DateFormatUtils.DATE_TIME_FORMAT);
}
/**
* 将时间或者时间日期字符串按照指定格式转换为Date
* @param dateStr
* @param pattern
* @return
*/
public static Date parseStrToCustomPatternDate(String dateStr, String pattern) {
if (checkPara(pattern) || checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
pattern);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串从一种格式转换成另一种格式.
* @param dateStr
* e.g. String dateStr = "2006-10-12 16:23:06";
* @param patternFrom
* e.g. DatePattern.DATE_TIME_FORMAT
* @param patternTo
* e.g. DatePattern.DATE_TIME_FORMAT_14
* @return
*/
public static String convertDatePattern(String dateStr,
String patternFrom, String patternTo) {
if (checkPara(patternFrom) || checkPara(patternTo) || checkPara(dateStr)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
patternFrom);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return formatDate(resDate,patternTo);
}
/**
* 日期天数增加
* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int days) {
if (checkPara(date)) {
return null ;
}
if ( 0 == days) {
return date;
}
Locale loc = Locale.getDefault();
Calendar cal = new GregorianCalendar(loc);
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH,days);
return cal.getTime();
}
/**
* 日期天数减少
* @param date
* @param days
* @return
*/
public static Date minusDays(Date date, int days) {
return addDays(date,( 0 - days));
}
/**
* 按时间格式相加
* @param dateStr 原来的时间
* @param pattern 时间格式
* @param type
* "year"年、"month"月、"day"日、"week"周、
* "hour"时、"minute"分、"second"秒
* 通过常量来设置,e.g.DateFormatUtils.YEAR
* @param count 相加数量
* @return
*/
public static String addDate(String dateStr, String pattern,
String type, int count) {
if (checkPara(dateStr) || checkPara(pattern) || checkPara(type)) {
return "" ;
}
if ( 0 == count) {
return dateStr;
}
Date date = parseStrToCustomPatternDate(dateStr,pattern);
Locale loc = Locale.getDefault();
Calendar cal = new GregorianCalendar(loc);
cal.setTime(date);
if (DateFormatUtils.YEAR.equals(type)) {
cal.add(Calendar.YEAR,count);
} else if (DateFormatUtils.MONTH.equals(type)) {
cal.add(Calendar.MONTH,count);
} else if (DateFormatUtils.DAY.equals(type)) {
cal.add(Calendar.DAY_OF_MONTH,count);
} else if (DateFormatUtils.WEEK.equals(type)) {
cal.add(Calendar.WEEK_OF_MONTH,count);
} else if (DateFormatUtils.HOUR.equals(type)) {
cal.add(Calendar.HOUR,count);
} else if (DateFormatUtils.MINUTE.equals(type)) {
cal.add(Calendar.MINUTE,count);
} else if (DateFormatUtils.SECOND.equals(type)) {
cal.add(Calendar.SECOND,count);
} else {
return "" ;
}
return formatDate(cal.getTime(),pattern);
}
/**
* 那时间格式相减
* @param dateStr
* @param pattern
* @param type
* @param count
* @return
*/
public static String minusDate(String dateStr, String pattern,
String type, int count) {
return addDate(dateStr,pattern,type,( 0 - count));
}
/**
* 日期大小比较
* @param dateStr1
* @param dateStr2
* @param pattern
* @return
*/
public static int compareDate(String dateStr1, String dateStr2, String pattern) {
if (checkPara(dateStr1) || checkPara(dateStr2) || checkPara(pattern)) {
return 888 ;
}
Date date1 = DateFormatUtils.parseStrToCustomPatternDate(dateStr1,pattern);
Date date2 = DateFormatUtils.parseStrToCustomPatternDate(dateStr2,pattern);
return date1.compareTo(date2);
}
/**
* 获得这个月的第一天
* @param dateStr
* @return
*/
public static String getFirstDayInMonth(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH,firstDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这个月的最后一天
* @param dateStr
* @return
*/
public static String getLastDayInMonth(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH,lastDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这周的第一天
* @param dateStr
* @return
*/
public static String getFirstDayInWeek(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_WEEK,firstDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这周的最后一天
* @param dateStr
* @return
*/
public static String getLastDayInWeek(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_WEEK,lastDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
}
文件2:DateFormatTest.java
package Common.Utils.DateUtils;;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
*
* <p>Description: 测试DateFormat</p>
* <p>Copyright 2006 mjn.</p>
* @author ma jia nan
* @Create Date : 2006-10-12
* @Version : 1.0
*/
public class DateFormatTest {
public static void main(String[] args) {
long currentTimeMillis = DateFormatUtils.getCurrentTimeMillis();
System.out.println( " Test 1: currentTimeMillis = " + currentTimeMillis);
Date currentDate = DateFormatUtils.getCurrentDate();
System.out.println( " Tese 2: currentDate = " + currentDate);
String currentFormatDate = DateFormatUtils.getCurrentFormatDate();
System.out.println( " Test 3: currentFormatDate = " + currentFormatDate);
String currentFormatDateTime = DateFormatUtils.getCurrentFormatDateTime();
System.out.println( " Test 4: currentFormatDateTime = " + currentFormatDateTime);
String currentCustomFomatDateTime12 = DateFormatUtils.getCurrentCustomFormatDateTime( " yyyy-MM-dd HH:mm " );
System.out.println( " Test 5: currentCustomFomatDateTime12 = " + currentCustomFomatDateTime12);
String formatDate = DateFormatUtils.formatDate(currentDate, " yyMM " );
System.out.println( " Test 6: formatDate = " + formatDate);
String formatDate1 = DateFormatUtils.formatDate(currentDate, " yyyyMMdd " , new Locale( " zh " , " CN " , "" ));
System.out.println( " Test 7: formatDate1 = " + formatDate1);
Date strToDate = DateFormatUtils.parseStrToDate(currentFormatDateTime);
System.out.println( " Test 8: strToDate = " + strToDate);
Date strToDateTime = DateFormatUtils.parseStrToDateTime(currentFormatDateTime);
System.out.println( " Test 9: strToDateTime = " + strToDateTime);
Calendar cal = DateFormatUtils.parseStrToCalendar(currentFormatDateTime);
System.out.println( " Test 10: Calendar = " +
cal.get(Calendar.YEAR) + " 年 " + cal.get(Calendar.MONTH) + " 月 " +
cal.get(Calendar.DAY_OF_MONTH) + " 日 " +
cal.get(Calendar.HOUR_OF_DAY) + " : " +
cal.get(Calendar.MINUTE) + " : " + cal.get(Calendar.SECOND));
String dateTimeStr = DateFormatUtils.parseDateStrToDateTimeStr(currentFormatDate);
System.out.println( " Test 11: DateStr = " + currentFormatDate +
" to DateTimeStr = " + dateTimeStr);
Date strToFormatDate = DateFormatUtils.parseStrToCustomPatternDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT);
Date strToFormatDateTime = DateFormatUtils.parseStrToCustomPatternDate(currentFormatDateTime, "" );
System.out.println( " Test 12: parse String to \n " +
" \t\tcustom pattern Date: " + strToFormatDate + " \n " +
" \t\tcustom pattern Date without pattern: " + strToFormatDateTime);
System.out.println( " Test 13: " + DateFormatUtils.formatDate(strToFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT));
String convertPattern = DateFormatUtils.convertDatePattern(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT,DateFormatUtils.DATE_TIME_FORMAT_14);
System.out.println( " Test 14: pattern from: " + currentFormatDateTime +
" \n\t\tpattern to: " + convertPattern);
Date addDays = DateFormatUtils.addDays(strToDate, 35 );
System.out.println( " Test 15: now -- " + strToDate +
" ,after add 30 days, \n\t the time is " + addDays);
Date minusDays = DateFormatUtils.minusDays(addDays, 35 );
System.out.println( " Test 16: now -- " + addDays +
" ,after minus 30 days, \n\t the time is " + minusDays);
String addYear = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " year " , 2 );
String addMonth = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " month " , 2 );
String addDay = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " day " , 2 );
String addHour = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " hour " , 2 );
String addMinute = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " minute " , 2 );
String minusWeek = DateFormatUtils.minusDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " week " , 2 );
String minusSecond = DateFormatUtils.minusDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " second " , 2 );
System.out.println( " Test 17: now -- " + currentFormatDateTime +
" \n\t add 2 years: " + addYear +
" \n\t add 2 month: " + addMonth +
" \n\t add 2 days: " + addDay +
" \n\t add 2 hours: " + addHour +
" \n\t add 2 minutes: " + addMinute +
" \n\t minus 2 weeks: " + minusWeek +
" \n\t minus 2 seconds: " + minusSecond);
System.out.println( " Test 18: " + " \n\t addMonth compare to addYear : " +
DateFormatUtils.compareDate(addMonth,addYear,DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addDay : " +
DateFormatUtils.compareDate(addMonth,addDay,DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addMonth : " +
DateFormatUtils.compareDate(addMonth,addMonth,DateFormatUtils.DATE_TIME_FORMAT));
String firstDayInMonth = DateFormatUtils.getFirstDayInMonth(currentFormatDateTime);
System.out.println( " Test 19: the first day in month is " + firstDayInMonth);
String lastDayInMonth = DateFormatUtils.getLastDayInMonth(currentFormatDateTime);
System.out.println( " Test 20: the last day in month is " + lastDayInMonth);
String firstDayInWeek = DateFormatUtils.getFirstDayInWeek(currentFormatDateTime);
System.out.println( " Test 21: the first day in week is " + firstDayInWeek);
String lastDayInWeek = DateFormatUtils.getLastDayInWeek(currentFormatDateTime);
System.out.println( " Test 21: the last day in week is " + lastDayInWeek);
}
}
我的输出结果:
Test 1 : currentTimeMillis = 1160655659140
Tese 2 : currentDate = Thu Oct 12 20 : 20 : 59 CST 2006
Test 3 : currentFormatDate = 2006 - 10 - 12
Test 4 : currentFormatDateTime = 2006 - 10 - 12 20 : 20 : 59
Test 5 : currentCustomFomatDateTime12 = 2006 - 10 - 12 20 : 20
Test 6 : formatDate = 0610
Test 7 : formatDate1 = 20061012
Test 8 : strToDate = Thu Oct 12 00 : 00 : 00 CST 2006
Test 9 : strToDateTime = Thu Oct 12 20 : 20 : 59 CST 2006
Test 10 : Calendar = 2006年9月12日20: 20 : 59
Test 11 : DateStr = 2006 - 10 - 12 to DateTimeStr = 2006 - 10 - 12 00 : 00 : 00
Test 12 : parse String to
custom pattern Date: Thu Oct 12 20 : 20 : 59 CST 2006
custom pattern Date without pattern: null
Test 13 :
Test 14 : pattern from: 2006 - 10 - 12 20 : 20 : 59
pattern to: 20061012202059
Test 15 : now -- Thu Oct 12 00 : 00 : 00 CST 2006 ,after add 30 days,
the time is Thu Nov 16 00 : 00 : 00 CST 2006
Test 16 : now -- Thu Nov 16 00 : 00 : 00 CST 2006 ,after minus 30 days,
the time is Thu Oct 12 00 : 00 : 00 CST 2006
Test 17 : now -- 2006-10-12 20:20:59
add 2 years: 2008-10-12 20:20:59
add 2 month: 2006 - 12 - 12 20 : 20 : 59
add 2 days: 2006-10-14 20:20:59
add 2 hours: 2006-10-12 22:20:59
add 2 minutes: 2006 - 10 - 12 20 : 22 : 59
minus 2 weeks: 2006 - 09 - 28 20 : 20 : 59
minus 2 seconds: 2006 - 10 - 12 20 : 20 : 57
Test 18 :
addMonth compare to addYear : - 1
addMonth compare to addDay : 1
addMonth compare to addMonth : 0
Test 19 : the first day in month is 2006 - 10 - 01
Test 20 : the last day in month is 2006 - 10 - 31
Test 21 : the first day in week is 2006 - 10 - 08
Test 21 : the last day in week is 2006 - 10 - 14
欢迎多提宝贵意见
一共2个文件:
DateFormat.java --- 时间以及时间格式相关的处理功能
DateFormatTest.java --- 测试文件
主要内容:
时间以及时间格式相关的处理功能,主要是字符串与时间之间的相互转换。
主要功能:
1. long getCurrentTimeMillis()
获得系统的当前时间,毫秒.
例如: 1160655659140
2. Date getCurrentDate()
获得系统的当前时间
例如:Thu Oct 12 20:20:59 CST 2006
3. String getCurrentFormatDate()
获得系统当前日期,以默认格式显示
例如:2006-10-12
4. String getCurrentFormatDateTime()
获得系统当前日期时间,以默认格式显示
例如:2006-10-12 20:20:59
5. String getCurrentCustomFormatDateTime(String pattern)
获得系统当前日期时间,按照指定格式返回
例如:par:pattern = "yyyy-MM-dd HH:mm"
res:2006-10-12 20:20
6. String formatDate(Date date, String pattern)
输入日期,按照指定格式返回
例如:par:pattern = "yyMM"
res: 0610
7. String formatDate(Date date, String pattern, Locale locale)
输入日期,按照指定格式返回
例如:par:pattern = "yyyyMMdd"
locale = new Locale("zh","CN","") /*中国地区*/
res: 20061012
8. Date parseStrToDate(String dateStr)
将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Date
9. Date parseStrToDateTime(String dateStr)
将时间字符串按照默认格式DATE_TIME_FORMAT ="yyyy-MM-dd HH:mm:ss",转换为Date
10. Calendar parseStrToCalendar(String dateStr)
将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Calender
11. String parseDateStrToDateTimeStr(String dateStr)
将日期字符串转换成日期时间字符串
12. Date parseStrToCustomPatternDate(String dateStr, String pattern)
将时间或者时间日期字符串按照指定格式转换为Date
13. String convertDatePattern(String dateStr, String patternFrom, String patternTo)
将时间字符串从一种格式转换成另一种格式
14. Date addDays(Date date, int days)
日期天数增加
15. Date minusDays(Date date, int days)
日期天数减少
16. String addDate(String dateStr, String pattern, String type, int count)
按时间格式相加
17. String minusDate(String dateStr, String pattern, String type, int count)
按时间格式相减
18. int compareDate(String dateStr1, String dateStr2, String pattern)
日期大小比较
19. String getFirstDayInMonth(String dateStr)
获得这个月的第一天
20. String getLastDayInMonth(String dateStr)
获得这个月的最后一天
21. String getFirstDayInWeek(String dateStr)
获得这周的第一天
22. String getLastDayInWeek(String dateStr)
获得这周的最后一天
文件1:DateFormat.java
package Common.Utils.DateUtils;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
*
* <p>Description: 时间以及时间格式相关的处理功能</p>
* <p>Copyright 2006.</p>
* @author ma jia nan
* @Create Date : 2006-10-12
* @Version : 1.0
*/
public class DateFormat {
// 系统默认日期格式
public static final String DATE_FORMAT = " yyyy-MM-dd " ;
// 系统默认日起时间格式
public static final String DATE_TIME_FORMAT = " yyyy-MM-dd HH:mm:ss " ;
// 8位日期格式
public static final String DATE_FORMAT_8 = " yyyyMMdd " ;
// 14为日期时间格式
public static final String DATE_TIME_FORMAT_14 = " yyyyMMddHHmmss " ;
public final static String YEAR = " year " ;
public final static String MONTH = " month " ;
public final static String DAY = " day " ;
public final static String WEEK = " week " ;
public final static String HOUR = " hour " ;
public final static String MINUTE = " minute " ;
public final static String SECOND = " second " ;
/**
* 判断参数是否等于null或者空
* @param para
* @return
*/
private static boolean checkPara(Object para) {
if ( null == para || "" .equals(para)) {
return true ;
} else {
return false ;
}
}
/**
* 获得系统的当前时间,毫秒.
* @return
*/
public static long getCurrentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 获得系统的当前时间
* @return
* e.g.Thu Oct 12 10:25:14 CST 2006
*/
public static Date getCurrentDate() {
// return new Date(System.currentTimeMillis());
return new Date(getCurrentTimeMillis());
}
/**
* 获得系统当前日期,以默认格式显示
* @return
* e.g.2006-10-12
*/
public static String getCurrentFormatDate() {
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
return dateFormator.format(currentDate);
}
/**
* 获得系统当前日期时间,以默认格式显示
* @return
* e.g.2006-10-12 10:55:06
*/
public static String getCurrentFormatDateTime() {
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
return dateFormator.format(currentDate);
}
/**
* 获得系统当前日期时间,按照指定格式返回
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者介于二者之间的格式,e.g."yyyyMMddHH"
* @return
* e.g.200610121115
*/
public static String getCurrentCustomFormatDateTime(String pattern) {
if (checkPara(pattern)) {
return "" ;
}
Date currentDate = getCurrentDate();
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern);
return dateFormator.format(currentDate);
}
/**
* 输入日期,按照指定格式返回
* @param date
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者类似于二者的格式,e.g."yyyyMMddHH","yyyyMM"
* @return
*/
public static String formatDate(Date date, String pattern) {
if (checkPara(pattern) || checkPara(date)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern);
return dateFormator.format(date);
}
/**
* 输入日期,按照指定格式返回
* @param date
* @param pattern
* e.g.DATE_FORMAT_8 = "yyyyMMdd";
* DATE_TIME_FORMAT_14 = "yyyyMMddHHmmss";
* 或者类似于二者的格式,e.g."yyyyMMddHH","yyyyMM"
* @param loc 国家地区,
* e.g.new Locale("zh","CN","") 中国
* Locale.getDefault();
* @return
*/
public static String formatDate(Date date, String pattern, Locale locale) {
if (checkPara(pattern) || checkPara(date)) {
return "" ;
}
if (checkPara(locale)) {
locale = Locale.getDefault();
}
SimpleDateFormat dateFormator = new SimpleDateFormat(pattern, locale);
String customFormatDate = dateFormator.format(date);
return customFormatDate;
}
/**
* 将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Date
* @param dateStr
* @return
*/
public static Date parseStrToDate(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串按照默认格式DATE_TIME_FORMAT ="yyyy-MM-dd HH:mm:ss",转换为Date
* @param dateStr
* @return
*/
public static Date parseStrToDateTime(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串按照默认格式DATE_FORMAT = "yyyy-MM-dd",转换为Calender
* @param dateStr
* @return
*/
public static Calendar parseStrToCalendar(String dateStr) {
if (checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_TIME_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
Locale locale = Locale.getDefault();
Calendar cal = new GregorianCalendar(locale);
cal.setTime(resDate);
return cal;
}
/**
* 将日期字符串转换成日期时间字符串
* @param dateStr
* @return
*/
public static String parseDateStrToDateTimeStr(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
DateFormatUtils.DATE_FORMAT);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return formatDate(resDate,DateFormatUtils.DATE_TIME_FORMAT);
}
/**
* 将时间或者时间日期字符串按照指定格式转换为Date
* @param dateStr
* @param pattern
* @return
*/
public static Date parseStrToCustomPatternDate(String dateStr, String pattern) {
if (checkPara(pattern) || checkPara(dateStr)) {
return null ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
pattern);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return resDate;
}
/**
* 将时间字符串从一种格式转换成另一种格式.
* @param dateStr
* e.g. String dateStr = "2006-10-12 16:23:06";
* @param patternFrom
* e.g. DatePattern.DATE_TIME_FORMAT
* @param patternTo
* e.g. DatePattern.DATE_TIME_FORMAT_14
* @return
*/
public static String convertDatePattern(String dateStr,
String patternFrom, String patternTo) {
if (checkPara(patternFrom) || checkPara(patternTo) || checkPara(dateStr)) {
return "" ;
}
SimpleDateFormat dateFormator = new SimpleDateFormat(
patternFrom);
Date resDate = dateFormator.parse(dateStr, new ParsePosition( 0 ));
return formatDate(resDate,patternTo);
}
/**
* 日期天数增加
* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int days) {
if (checkPara(date)) {
return null ;
}
if ( 0 == days) {
return date;
}
Locale loc = Locale.getDefault();
Calendar cal = new GregorianCalendar(loc);
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH,days);
return cal.getTime();
}
/**
* 日期天数减少
* @param date
* @param days
* @return
*/
public static Date minusDays(Date date, int days) {
return addDays(date,( 0 - days));
}
/**
* 按时间格式相加
* @param dateStr 原来的时间
* @param pattern 时间格式
* @param type
* "year"年、"month"月、"day"日、"week"周、
* "hour"时、"minute"分、"second"秒
* 通过常量来设置,e.g.DateFormatUtils.YEAR
* @param count 相加数量
* @return
*/
public static String addDate(String dateStr, String pattern,
String type, int count) {
if (checkPara(dateStr) || checkPara(pattern) || checkPara(type)) {
return "" ;
}
if ( 0 == count) {
return dateStr;
}
Date date = parseStrToCustomPatternDate(dateStr,pattern);
Locale loc = Locale.getDefault();
Calendar cal = new GregorianCalendar(loc);
cal.setTime(date);
if (DateFormatUtils.YEAR.equals(type)) {
cal.add(Calendar.YEAR,count);
} else if (DateFormatUtils.MONTH.equals(type)) {
cal.add(Calendar.MONTH,count);
} else if (DateFormatUtils.DAY.equals(type)) {
cal.add(Calendar.DAY_OF_MONTH,count);
} else if (DateFormatUtils.WEEK.equals(type)) {
cal.add(Calendar.WEEK_OF_MONTH,count);
} else if (DateFormatUtils.HOUR.equals(type)) {
cal.add(Calendar.HOUR,count);
} else if (DateFormatUtils.MINUTE.equals(type)) {
cal.add(Calendar.MINUTE,count);
} else if (DateFormatUtils.SECOND.equals(type)) {
cal.add(Calendar.SECOND,count);
} else {
return "" ;
}
return formatDate(cal.getTime(),pattern);
}
/**
* 那时间格式相减
* @param dateStr
* @param pattern
* @param type
* @param count
* @return
*/
public static String minusDate(String dateStr, String pattern,
String type, int count) {
return addDate(dateStr,pattern,type,( 0 - count));
}
/**
* 日期大小比较
* @param dateStr1
* @param dateStr2
* @param pattern
* @return
*/
public static int compareDate(String dateStr1, String dateStr2, String pattern) {
if (checkPara(dateStr1) || checkPara(dateStr2) || checkPara(pattern)) {
return 888 ;
}
Date date1 = DateFormatUtils.parseStrToCustomPatternDate(dateStr1,pattern);
Date date2 = DateFormatUtils.parseStrToCustomPatternDate(dateStr2,pattern);
return date1.compareTo(date2);
}
/**
* 获得这个月的第一天
* @param dateStr
* @return
*/
public static String getFirstDayInMonth(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH,firstDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这个月的最后一天
* @param dateStr
* @return
*/
public static String getLastDayInMonth(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH,lastDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这周的第一天
* @param dateStr
* @return
*/
public static String getFirstDayInWeek(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_WEEK,firstDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
/**
* 获得这周的最后一天
* @param dateStr
* @return
*/
public static String getLastDayInWeek(String dateStr) {
if (checkPara(dateStr)) {
return "" ;
}
Calendar cal = DateFormatUtils.parseStrToCalendar(dateStr);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_WEEK,lastDay);
return DateFormatUtils.formatDate(cal.getTime(),DateFormatUtils.DATE_FORMAT);
}
}
文件2:DateFormatTest.java
package Common.Utils.DateUtils;;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
*
* <p>Description: 测试DateFormat</p>
* <p>Copyright 2006 mjn.</p>
* @author ma jia nan
* @Create Date : 2006-10-12
* @Version : 1.0
*/
public class DateFormatTest {
public static void main(String[] args) {
long currentTimeMillis = DateFormatUtils.getCurrentTimeMillis();
System.out.println( " Test 1: currentTimeMillis = " + currentTimeMillis);
Date currentDate = DateFormatUtils.getCurrentDate();
System.out.println( " Tese 2: currentDate = " + currentDate);
String currentFormatDate = DateFormatUtils.getCurrentFormatDate();
System.out.println( " Test 3: currentFormatDate = " + currentFormatDate);
String currentFormatDateTime = DateFormatUtils.getCurrentFormatDateTime();
System.out.println( " Test 4: currentFormatDateTime = " + currentFormatDateTime);
String currentCustomFomatDateTime12 = DateFormatUtils.getCurrentCustomFormatDateTime( " yyyy-MM-dd HH:mm " );
System.out.println( " Test 5: currentCustomFomatDateTime12 = " + currentCustomFomatDateTime12);
String formatDate = DateFormatUtils.formatDate(currentDate, " yyMM " );
System.out.println( " Test 6: formatDate = " + formatDate);
String formatDate1 = DateFormatUtils.formatDate(currentDate, " yyyyMMdd " , new Locale( " zh " , " CN " , "" ));
System.out.println( " Test 7: formatDate1 = " + formatDate1);
Date strToDate = DateFormatUtils.parseStrToDate(currentFormatDateTime);
System.out.println( " Test 8: strToDate = " + strToDate);
Date strToDateTime = DateFormatUtils.parseStrToDateTime(currentFormatDateTime);
System.out.println( " Test 9: strToDateTime = " + strToDateTime);
Calendar cal = DateFormatUtils.parseStrToCalendar(currentFormatDateTime);
System.out.println( " Test 10: Calendar = " +
cal.get(Calendar.YEAR) + " 年 " + cal.get(Calendar.MONTH) + " 月 " +
cal.get(Calendar.DAY_OF_MONTH) + " 日 " +
cal.get(Calendar.HOUR_OF_DAY) + " : " +
cal.get(Calendar.MINUTE) + " : " + cal.get(Calendar.SECOND));
String dateTimeStr = DateFormatUtils.parseDateStrToDateTimeStr(currentFormatDate);
System.out.println( " Test 11: DateStr = " + currentFormatDate +
" to DateTimeStr = " + dateTimeStr);
Date strToFormatDate = DateFormatUtils.parseStrToCustomPatternDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT);
Date strToFormatDateTime = DateFormatUtils.parseStrToCustomPatternDate(currentFormatDateTime, "" );
System.out.println( " Test 12: parse String to \n " +
" \t\tcustom pattern Date: " + strToFormatDate + " \n " +
" \t\tcustom pattern Date without pattern: " + strToFormatDateTime);
System.out.println( " Test 13: " + DateFormatUtils.formatDate(strToFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT));
String convertPattern = DateFormatUtils.convertDatePattern(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT,DateFormatUtils.DATE_TIME_FORMAT_14);
System.out.println( " Test 14: pattern from: " + currentFormatDateTime +
" \n\t\tpattern to: " + convertPattern);
Date addDays = DateFormatUtils.addDays(strToDate, 35 );
System.out.println( " Test 15: now -- " + strToDate +
" ,after add 30 days, \n\t the time is " + addDays);
Date minusDays = DateFormatUtils.minusDays(addDays, 35 );
System.out.println( " Test 16: now -- " + addDays +
" ,after minus 30 days, \n\t the time is " + minusDays);
String addYear = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " year " , 2 );
String addMonth = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " month " , 2 );
String addDay = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " day " , 2 );
String addHour = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " hour " , 2 );
String addMinute = DateFormatUtils.addDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " minute " , 2 );
String minusWeek = DateFormatUtils.minusDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " week " , 2 );
String minusSecond = DateFormatUtils.minusDate(currentFormatDateTime,DateFormatUtils.DATE_TIME_FORMAT, " second " , 2 );
System.out.println( " Test 17: now -- " + currentFormatDateTime +
" \n\t add 2 years: " + addYear +
" \n\t add 2 month: " + addMonth +
" \n\t add 2 days: " + addDay +
" \n\t add 2 hours: " + addHour +
" \n\t add 2 minutes: " + addMinute +
" \n\t minus 2 weeks: " + minusWeek +
" \n\t minus 2 seconds: " + minusSecond);
System.out.println( " Test 18: " + " \n\t addMonth compare to addYear : " +
DateFormatUtils.compareDate(addMonth,addYear,DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addDay : " +
DateFormatUtils.compareDate(addMonth,addDay,DateFormatUtils.DATE_TIME_FORMAT) +
" \n\t addMonth compare to addMonth : " +
DateFormatUtils.compareDate(addMonth,addMonth,DateFormatUtils.DATE_TIME_FORMAT));
String firstDayInMonth = DateFormatUtils.getFirstDayInMonth(currentFormatDateTime);
System.out.println( " Test 19: the first day in month is " + firstDayInMonth);
String lastDayInMonth = DateFormatUtils.getLastDayInMonth(currentFormatDateTime);
System.out.println( " Test 20: the last day in month is " + lastDayInMonth);
String firstDayInWeek = DateFormatUtils.getFirstDayInWeek(currentFormatDateTime);
System.out.println( " Test 21: the first day in week is " + firstDayInWeek);
String lastDayInWeek = DateFormatUtils.getLastDayInWeek(currentFormatDateTime);
System.out.println( " Test 21: the last day in week is " + lastDayInWeek);
}
}
我的输出结果:
Test 1 : currentTimeMillis = 1160655659140
Tese 2 : currentDate = Thu Oct 12 20 : 20 : 59 CST 2006
Test 3 : currentFormatDate = 2006 - 10 - 12
Test 4 : currentFormatDateTime = 2006 - 10 - 12 20 : 20 : 59
Test 5 : currentCustomFomatDateTime12 = 2006 - 10 - 12 20 : 20
Test 6 : formatDate = 0610
Test 7 : formatDate1 = 20061012
Test 8 : strToDate = Thu Oct 12 00 : 00 : 00 CST 2006
Test 9 : strToDateTime = Thu Oct 12 20 : 20 : 59 CST 2006
Test 10 : Calendar = 2006年9月12日20: 20 : 59
Test 11 : DateStr = 2006 - 10 - 12 to DateTimeStr = 2006 - 10 - 12 00 : 00 : 00
Test 12 : parse String to
custom pattern Date: Thu Oct 12 20 : 20 : 59 CST 2006
custom pattern Date without pattern: null
Test 13 :
Test 14 : pattern from: 2006 - 10 - 12 20 : 20 : 59
pattern to: 20061012202059
Test 15 : now -- Thu Oct 12 00 : 00 : 00 CST 2006 ,after add 30 days,
the time is Thu Nov 16 00 : 00 : 00 CST 2006
Test 16 : now -- Thu Nov 16 00 : 00 : 00 CST 2006 ,after minus 30 days,
the time is Thu Oct 12 00 : 00 : 00 CST 2006
Test 17 : now -- 2006-10-12 20:20:59
add 2 years: 2008-10-12 20:20:59
add 2 month: 2006 - 12 - 12 20 : 20 : 59
add 2 days: 2006-10-14 20:20:59
add 2 hours: 2006-10-12 22:20:59
add 2 minutes: 2006 - 10 - 12 20 : 22 : 59
minus 2 weeks: 2006 - 09 - 28 20 : 20 : 59
minus 2 seconds: 2006 - 10 - 12 20 : 20 : 57
Test 18 :
addMonth compare to addYear : - 1
addMonth compare to addDay : 1
addMonth compare to addMonth : 0
Test 19 : the first day in month is 2006 - 10 - 01
Test 20 : the last day in month is 2006 - 10 - 31
Test 21 : the first day in week is 2006 - 10 - 08
Test 21 : the last day in week is 2006 - 10 - 14