package com.mall.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
public class DateUtils {
private static Logger log=Logger.getLogger(DateUtils.class);
/**
* 获取昨天日期
*
* @param date
* @return
* ace
*/
public static String getYesterDay(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60;
date.setTime(beforeTime * 1000);
return formatter.format(date);
}
/**
* 获取昨天日期(返回数值)
*
* @param date
* @return num
*/
public static Integer getYesterDayNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60;
date.setTime(beforeTime * 1000);
return Integer.parseInt(formatter.format(date));
}
/**
* 获取一周前的日期(当前日期往前推7天)
*
* @param date
* @return
*/
public static String getWeekdayBeforeDate(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7;
date.setTime(beforeTime * 1000);
return formatter.format(date);
}
/**
* 获取一周前的日期(当前日期往前推7天)(返回数值)
*
* @param date
* @return
*/
public static Integer getWeekdayBeforeDateNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 7;
date.setTime(beforeTime * 1000);
return Integer.parseInt(formatter.format(date));
}
/**
* 获取一月前的日期(当前日期往前推30天)
*
* @param date
* @return
*/
public static String getMonthBeforeDate(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30;
date.setTime(beforeTime * 1000);
return formatter.format(date);
}
/**
* 获取一月前的日期(当前日期往前推30天)(返回数值)
*
* @param date
* @return
*/
public static Integer getMonthBeforeDateNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30;
date.setTime(beforeTime * 1000);
return Integer.parseInt(formatter.format(date));
}
/**
* 获取三月前的日期(当前日期往前推90天)
*
* @param date
* @return
*/
public static String get3MonthBeforeDate(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3;
date.setTime(beforeTime * 1000);
String d = formatter.format(date);
return d;
}
/**
* 获取三月前的日期(当前日期往前推30天)(返回数值)
*
* @param date
* @return
*/
public static Integer get3MonthBeforeDateNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
long beforeTime = (date.getTime() / 1000) - 24 * 60 * 60 * 30 * 3;
date.setTime(beforeTime * 1000);
return Integer.parseInt(formatter.format(date));
}
/**
* 获取一年后的日期
*
* @return
*/
public static String getNextYear(int chooseYear, Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
long beforeTime = (date.getTime() / 1000) + 60 * 60 * 24 * 365
* chooseYear;
date.setTime(beforeTime * 1000);
return formatter.format(date);
}
/**
* 日期转换成字符串
*
* @param date
* @return
*/
public static String convertDateToStr(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
return formatter.format(date);
}
/**
* 日期转换成字符串 yyyy-MM-dd HHmmss
*
* @param date
* @return
*/
public static String convertTimeToStr(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return formatter.format(date);
}
/**
* 将日期转为数值
*
* @param date
* @return
*/
public static Integer convertDateToNum(Date date) {
java.text.Format formatter = new java.text.SimpleDateFormat("yyyyMMdd");
return Integer.parseInt(formatter.format(date));
}
/**
* 将字符串日期转为数值
*
* @param date
* @return
*/
public static Integer convertStrToNum(String date) {
if (date.contains("-")) {
date = date.replace("-", "");
} else if (date.contains(".")) {
date = date.replace(".", "");
} else if (date.contains("/")) {
date = date.replace("/", "");
}
return Integer.parseInt(date);
}
/**
* 时间转换器 第一个参数 要转化的数据类型 --- java.util.Date 第二个参数 要转化的数据 --- "2010-12-12"
*
*/
public static Date convertStrTODate( String str,Class<Date> type,String datePattern) {
if (str == null) {
return null;
} else {
if (type == java.util.Date.class) {
if (str instanceof String) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
return sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException("您输入的数据格式不对");
}
} else {
throw new RuntimeException("您要转化的数据输入不是String类型");
}
} else {
throw new RuntimeException("您要转化的数据类型不对");
}
}
}
/**
* 根据生日计算年龄
*
* @param birthDay
* @return
* @throws Exception
*/
public static int getAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
// monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
} else {
// do nothing
}
} else {
// monthNow>monthBirth
age--;
}
} else {
// monthNow<monthBirth
// donothing
}
return age;
}
///////////////////////////////////////根据时间段获取时间的集合//////////////////////////////////////////////////////////////////
public static List<String> getDateListBydates(String s1,String s2,String format) throws ParseException{
List<String>list=new ArrayList<String>();
//String s1 = "2012-02-01";
//String s2 = "2012-04-04";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date begin=sdf.parse(s1);
Date end=sdf.parse(s2);
double between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
double day=between/(24*3600);
for(int i = 0;i<=day;i++){
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(s1));
cd.add(Calendar.DATE, i);//增加一天
//cd.add(Calendar.MONTH, n);//增加一个月
log.info(sdf.format(cd.getTime()));
list.add(sdf.format(cd.getTime()));
}
return list;
}
//获取指定年月的总天数
public static int getLastDay(int year, int month) {
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int last = cal.getActualMaximum(Calendar.DATE);
System.out.println(last);
return last;
}
//获取指定年月的日期
@SuppressWarnings("unchecked")
public static List<String> getDatesByMonth(int year, int month){
List<String> list=new ArrayList();
String yyyy=year+"";
String mm=month+"";
String dd="01";
if(month<10){
mm="0"+month;
}
int num=getLastDay(year, month);
for(int i=1;i<=num;i++){
if(i<10){
dd="0"+i;
}else{
dd=i+"";
}
list.add(yyyy+"-"+mm+"-"+dd);
System.out.println(yyyy+"-"+mm+"-"+dd);
}
return list;
}
/**
*
* @param datestr 解析字符串 如2014-04-1716:38:57
* @param sourceDateformat 源日期格式 如yyyy-MM-ddHH:mm:ss
* @param formatStr 要转换的日期格式 如(yyyy-MM-dd HH:mm:ss)
* @return
*/
public static String strTOdateTOStr(String datestr,String sourceDateformat,String targetDateformat){
// String str="2014-04-1716:38:57";
try {
SimpleDateFormat sourceFormat = new SimpleDateFormat(sourceDateformat);
SimpleDateFormat targetFormat = new SimpleDateFormat(targetDateformat);
return targetFormat.format(sourceFormat.parse(datestr));
} catch (Exception e) {
log.info("strTOdateTOStr:"+e);
}
return null;
}
public static void main(String[] args) throws Exception{
//getDatesByMonth(2013,2);
getDateListBydates("20130517","20130519","yyyyMMdd");
}
public class DateUtil { //默认显示日期的格式 public static final String DATAFORMAT_STR = "yyyy-MM-dd"; //默认显示日期的格式 public static final String YYYY_MM_DATAFORMAT_STR = "yyyy-MM"; //默认显示日期时间的格式 public static final String DATATIMEF_STR = "yyyy-MM-dd HH:mm:ss"; //默认显示简体中文日期的格式 public static final String ZHCN_DATAFORMAT_STR = "yyyy年MM月dd日"; //默认显示简体中文日期时间的格式 public static final String ZHCN_DATATIMEF_STR = "yyyy年MM月dd日HH时mm分ss秒"; //默认显示简体中文日期时间的格式 public static final String ZHCN_DATATIMEF_STR_4yMMddHHmm = "yyyy年MM月dd日HH时mm分"; private static DateFormat dateFormat = null; private static DateFormat dateTimeFormat = null; private static DateFormat zhcnDateFormat = null; private static DateFormat zhcnDateTimeFormat = null; static { dateFormat = new SimpleDateFormat(DATAFORMAT_STR); dateTimeFormat = new SimpleDateFormat(DATATIMEF_STR); zhcnDateFormat = new SimpleDateFormat(ZHCN_DATAFORMAT_STR); zhcnDateTimeFormat = new SimpleDateFormat(ZHCN_DATATIMEF_STR); } private static DateFormat getDateFormat(String formatStr) { if (formatStr.equalsIgnoreCase(DATAFORMAT_STR)) { return dateFormat; } else if (formatStr.equalsIgnoreCase(DATATIMEF_STR)) { return dateTimeFormat; } else if (formatStr.equalsIgnoreCase(ZHCN_DATAFORMAT_STR)) { return zhcnDateFormat; } else if (formatStr.equalsIgnoreCase(ZHCN_DATATIMEF_STR)) { return zhcnDateTimeFormat; } else { return new SimpleDateFormat(formatStr); } } /** * 按照默认显示日期时间的格式"yyyy-MM-dd HH:mm:ss",转化dateTimeStr为Date类型 * dateTimeStr必须是"yyyy-MM-dd HH:mm:ss"的形式 * @param dateTimeStr * @return */ public static Date getDate(String dateTimeStr) { return getDate(dateTimeStr, DATATIMEF_STR); } /** * 按照默认formatStr的格式,转化dateTimeStr为Date类型 * dateTimeStr必须是formatStr的形式 * @param dateTimeStr * @param formatStr * @return */ public static Date getDate(String dateTimeStr, String formatStr) { try { if (dateTimeStr == null || dateTimeStr.equals("")) { return null; } DateFormat sdf = getDateFormat(formatStr); java.util.Date d = sdf.parse(dateTimeStr); return d; } catch (ParseException e) { throw new RuntimeException(e); } } /** * 将YYYYMMDD转换成Date日期 * @param date * @return * @throws BusinessException */ public static Date transferDate(String date) throws Exception { if (date == null || date.length() < 1) return null; if (date.length() != 8) throw new Exception("日期格式错误"); String con = "-"; String yyyy = date.substring(0, 4); String mm = date.substring(4, 6); String dd = date.substring(6, 8); int month = Integer.parseInt(mm); int day = Integer.parseInt(dd); if (month < 1 || month > 12 || day < 1 || day > 31) throw new Exception("日期格式错误"); String str = yyyy + con + mm + con + dd; return DateUtil.getDate(str, DateUtil.DATAFORMAT_STR); } /** * 将YYYY-MM-DD日期转换成yyyymmdd格式字符串 * @param date * @return */ public static String getYYYYMMDDDate(Date date) { if (date == null) return null; String yyyy = getYear(date) + ""; String mm = getMonth(date) + ""; String dd = getDay(date) + ""; mm = StringUtil.rightAlign(mm, 2, "0"); dd = StringUtil.rightAlign(dd, 2, "0"); return yyyy + mm + dd; } /** * 将YYYY-MM-DD日期转换成YYYYMMDDHHMMSS格式字符串 * @param date * @return */ public static String getYYYYMMDDHHMMSSDate(Date date) { if (date == null) return null; String yyyy = getYear(date) + ""; String mm = getMonth(date) + ""; String dd = getDay(date) + ""; String hh = getHour(date) + ""; String min = getMin(date) + ""; String ss = getSecond(date) + ""; mm = StringUtil.rightAlign(mm, 2, "0"); dd = StringUtil.rightAlign(dd, 2, "0"); hh = StringUtil.rightAlign(hh, 2, "0"); min = StringUtil.rightAlign(min, 2, "0"); ss = StringUtil.rightAlign(ss, 2, "0"); return yyyy + mm + dd + hh + min + ss; } /** * 将YYYY-MM-DD日期转换成yyyymmdd格式字符串 * @param date * @return */ public static String getYYYYMMDDDate(String date) { return getYYYYMMDDDate(getDate(date, DATAFORMAT_STR)); } /** * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串 * @param date * @return */ public static String dateToDateString(Date date) { return dateToDateString(date, DATATIMEF_STR); } /** * 将Date转换成formatStr格式的字符串 * @param date * @param formatStr * @return */ public static String dateToDateString(Date date, String formatStr) { DateFormat df = getDateFormat(formatStr); return df.format(date); } /** * 返回一个yyyy-MM-dd HH:mm:ss 形式的日期时间字符串中的HH:mm:ss * @param dateTime * @return */ public static String getTimeString(String dateTime) { return getTimeString(dateTime, DATATIMEF_STR); } /** * 返回一个formatStr格式的日期时间字符串中的HH:mm:ss * @param dateTime * @param formatStr * @return */ public static String getTimeString(String dateTime, String formatStr) { Date d = getDate(dateTime, formatStr); String s = dateToDateString(d); return s.substring(DATATIMEF_STR.indexOf('H')); } /** * 获取当前日期yyyy-MM-dd的形式 * @return */ public static String getCurDate() { //return dateToDateString(new Date(),DATAFORMAT_STR); return dateToDateString(Calendar.getInstance().getTime(), DATAFORMAT_STR); } /** * 获取当前日期yyyy年MM月dd日的形式 * @return */ public static String getCurZhCNDate() { return dateToDateString(new Date(), ZHCN_DATAFORMAT_STR); } /** * 获取当前日期时间yyyy-MM-dd HH:mm:ss的形式 * @return */ public static String getCurDateTime() { return dateToDateString(new Date(), DATATIMEF_STR); } /** * 获取当前日期时间yyyy年MM月dd日HH时mm分ss秒的形式 * @return */ public static String getCurZhCNDateTime() { return dateToDateString(new Date(), ZHCN_DATATIMEF_STR); } /** * 获取日期d的days天后的一个Date * @param d * @param days * @return */ public static Date getInternalDateByDay(Date d, int days) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.DATE, days); return now.getTime(); } public static Date getInternalDateByMon(Date d, int months) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.MONTH, months); return now.getTime(); } public static Date getInternalDateByYear(Date d, int years) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.YEAR, years); return now.getTime(); } public static Date getInternalDateBySec(Date d, int sec) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.SECOND, sec); return now.getTime(); } public static Date getInternalDateByMin(Date d, int min) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.MINUTE, min); return now.getTime(); } public static Date getInternalDateByHour(Date d, int hours) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); now.add(Calendar.HOUR_OF_DAY, hours); return now.getTime(); } /** * 根据一个日期字符串,返回日期格式,目前支持4种 * 如果都不是,则返回null * @param DateString * @return */ public static String getFormateStr(String DateString) { String patternStr1 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"; //"yyyy-MM-dd" String patternStr2 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"; //"yyyy-MM-dd HH:mm:ss"; String patternStr3 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日";//"yyyy年MM月dd日" String patternStr4 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日[0-9]{1,2}时[0-9]{1,2}分[0-9]{1,2}秒";//"yyyy年MM月dd日HH时mm分ss秒" Pattern p = Pattern.compile(patternStr1); Matcher m = p.matcher(DateString); boolean b = m.matches(); if (b) return DATAFORMAT_STR; p = Pattern.compile(patternStr2); m = p.matcher(DateString); b = m.matches(); if (b) return DATATIMEF_STR; p = Pattern.compile(patternStr3); m = p.matcher(DateString); b = m.matches(); if (b) return ZHCN_DATAFORMAT_STR; p = Pattern.compile(patternStr4); m = p.matcher(DateString); b = m.matches(); if (b) return ZHCN_DATATIMEF_STR; return null; } /** * 将一个"yyyy-MM-dd HH:mm:ss"字符串,转换成"yyyy年MM月dd日HH时mm分ss秒"的字符串 * @param dateStr * @return */ public static String getZhCNDateTime(String dateStr) { Date d = getDate(dateStr); return dateToDateString(d, ZHCN_DATATIMEF_STR); } /** * 将一个"yyyy-MM-dd"字符串,转换成"yyyy年MM月dd日"的字符串 * @param dateStr * @return */ public static String getZhCNDate(String dateStr) { Date d = getDate(dateStr, DATAFORMAT_STR); return dateToDateString(d, ZHCN_DATAFORMAT_STR); } /** * 将dateStr从fmtFrom转换到fmtTo的格式 * @param dateStr * @param fmtFrom * @param fmtTo * @return */ public static String getDateStr(String dateStr, String fmtFrom, String fmtTo) { Date d = getDate(dateStr, fmtFrom); return dateToDateString(d, fmtTo); } /** * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期,之间相差多少毫秒,time2-time1 * @param time1 * @param time2 * @return */ public static long compareDateStr(String time1, String time2) { Date d1 = getDate(time1); Date d2 = getDate(time2); return d2.getTime() - d1.getTime(); } /** * 将小时数换算成返回以毫秒为单位的时间 * @param hours * @return */ public static long getMicroSec(BigDecimal hours) { BigDecimal bd; bd = hours.multiply(new BigDecimal(3600 * 1000)); return bd.longValue(); } /** * 获取Date中的分钟 * @param d * @return */ public static int getMin(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.MINUTE); } /** * 获取Date中的小时(24小时) * @param d * @return */ public static int getHour(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.HOUR_OF_DAY); } /** * 获取Date中的秒 * @param d * @return */ public static int getSecond(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.SECOND); } /** * 获取xxxx-xx-xx的日 * @param d * @return */ public static int getDay(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.DAY_OF_MONTH); } /** * 获取月份,1-12月 * @param d * @return */ public static int getMonth(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.MONTH) + 1; } /** * 获取19xx,20xx形式的年 * @param d * @return */ public static int getYear(Date d) { Calendar now = Calendar.getInstance(TimeZone.getDefault()); now.setTime(d); return now.get(Calendar.YEAR); } /** * 得到d的上个月的年份+月份,如200505 * @return */ public static String getYearMonthOfLastMon(Date d) { Date newdate = getInternalDateByMon(d, -1); String year = String.valueOf(getYear(newdate)); String month = String.valueOf(getMonth(newdate)); return year + month; } /** * 得到当前日期的年和月如200509 * @return String */ public static String getCurYearMonth() { Calendar now = Calendar.getInstance(TimeZone.getDefault()); String DATE_FORMAT = "yyyyMM"; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT); sdf.setTimeZone(TimeZone.getDefault()); return (sdf.format(now.getTime())); } public static Date getNextMonth(String year, String month) { String datestr = year + "-" + month + "-01"; Date date = getDate(datestr, DATAFORMAT_STR); return getInternalDateByMon(date, 1); } public static Date getLastMonth(String year, String month) { String datestr = year + "-" + month + "-01"; Date date = getDate(datestr, DATAFORMAT_STR); return getInternalDateByMon(date, -1); } /** * 得到日期d,按照页面日期控件格式,如"2001-3-16" * @param d * @return */ public static String getSingleNumDate(Date d) { return dateToDateString(d, DATAFORMAT_STR); } /** * 得到d半年前的日期,"yyyy-MM-dd" * @param d * @return */ public static String getHalfYearBeforeStr(Date d) { return dateToDateString(getInternalDateByMon(d, -6), DATAFORMAT_STR); } /** * 得到当前日期D的月底的前/后若干天的时间,<0表示之前,>0表示之后 * @param d * @param days * @return */ public static String getInternalDateByLastDay(Date d, int days) { return dateToDateString(getInternalDateByDay(d, days), DATAFORMAT_STR); } /** * 日期中的年月日相加 * @param field int 需要加的字段 年 月 日 * @param amount int 加多少 * @return String */ public static String addDate(int field, int amount) { int temp = 0; if (field == 1) { temp = Calendar.YEAR; } if (field == 2) { temp = Calendar.MONTH; } if (field == 3) { temp = Calendar.DATE; } String Time = ""; try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(TimeZone.getDefault()); cal.add(temp, amount); Time = sdf.format(cal.getTime()); return Time; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 获得系统当前月份的天数 * @return */ public static int getCurentMonthDay() { Date date = Calendar.getInstance().getTime(); return getMonthDay(date); } /** * 获得指定日期月份的天数 * @return */ public static int getMonthDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.getActualMaximum(Calendar.DAY_OF_MONTH); } /** * 获得指定日期月份的天数 yyyy-mm-dd * @return */ public static int getMonthDay(String date) { Date strDate = getDate(date, DATAFORMAT_STR); return getMonthDay(strDate); } public static String getStringDate(Calendar cal) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(cal.getTime()); } /** * @param args */ public static void main(String[] args) { // //System.out.print(DateUtil.getDate("04:04:04","HH:mm:ss")); // System.out.print("\n"+DateUtil.getCurZhCNDateTime()); // System.out.print("\n"+getFormateStr(DateUtil.getCurDate())); // System.out.print("\n"+compareDateStr("1900-1-1 1:1:2","1900-1-1 1:1:3")); // System.out.print("\n"+getDay(new Date())); // System.out.print("\n"+getMonth(new Date())); // System.out.print("\n"+getYear(new Date())); // System.out.print("\n"+getMin(new Date())); //// System.out.print("\n"+new Date().getSeconds()); /*Date d1 = new Date(2007,11,30); Date d2 = new Date(2007,12,1); if(d2.compareTo(d1)>0){ System.out.println("d2大于d1"); }else{ System.out.println("d2小于d1"); }*/ System.out.println(addDate(1, 1)); System.out.println(addDate(2, 1)); System.out.println(addDate(3, 1)); System.out.println(getYYYYMMDDHHMMSSDate(new Date())); System.out.println(getCurentMonthDay()); } }
}
package com.grace.test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class showDate {
public static void main(String[] args) throws ParseException {
Date d = new Date();
String s = null;
/** 输出格式: Mon May 05 15:23:58 CST 2014 */
System.out.println(d);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/** 输出格式: 2014年5月5日 星期一 */
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/** 输出格式: 2014-5-5 */
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/** 输出格式: 14-5-5 */
s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);
/** 输出格式: 2014-5-05 00:00:00 大写H为24小时制 */
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
s = sdf.format(d);
System.out.println(s);
/** 输出格式: 2014-5-05 00:00:00 小写h为12小时制 */
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = sdf2.format(d);
System.out.println(s);
/** 输出格式: 20140505000000 */
DateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");
s = sdf3.format(d);
System.out.println(s);
/** 字符串转换城日期格式 */
s = sdf.format(d);
Date today = sdf.parse(s);
System.out.println("字符串转成日期1:" + today);
System.out.println("字符串转成日期2:" + sdf.format(today));
/** 单独输出年月日时分秒等 */
Calendar c = Calendar.getInstance();
System.out.println("年: " + c.get(Calendar.YEAR));
// 月份从0开始,加1校正
System.out.println("月: " + (c.get(Calendar.MONTH) + 1) + "");
System.out.println("日: " + c.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + c.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + c.get(Calendar.MINUTE));
System.out.println("秒: " + c.get(Calendar.SECOND));
System.out.println("当前时间毫秒数:" + c.getTimeInMillis());
System.out.println("当前时间: " + c.getTime());
}
}