/**
* ZhouJianGuo
* Dec 25, 2007
* MSN:zhoujianguo_leo@hotmail.com
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author zhoujianguo
* Dec 25, 2007
* Tenwa
* DateUtil.java
*/
public class DateUtil {
public final static int WeekSpan = 7;
public static int month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* 判断是否闰年*/
private static boolean LeapYear(int year) {
if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
//System.out.println(year+" is a leap year.");
return true;
}
else {
//System.out.println(year+" is not a leap year.");
return false;
}
}
/* 对个位数的月份之前补零 */
private static String impleMonth(int month) {
String monthStr = new Integer(month).toString();
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
return monthStr;
}
/* 对个位数的日子之前补零 */
private static String impleDay(int day) {
String dayStr = new Integer(day).toString();
if (dayStr.length() == 1) {
dayStr = "0" + dayStr;
}
return dayStr;
}
/* 得到当前周的第一天(周一)的日期*/
public static String getWeekFirstDate() {
Date da = new Date();
int dayOfWeek = da.getDay(); // 当前日期是星期几
long fromWeekFirstInMillis = (dayOfWeek - 1) * 24 * 60 * 60 * 1000; // 与该周第一天相隔的毫秒数
da.setTime(da.getTime() - fromWeekFirstInMillis);
String weekFirstDay = new Integer(da.getYear() + 1900).toString();
weekFirstDay = weekFirstDay + "-" + impleMonth(da.getMonth() + 1);
weekFirstDay = weekFirstDay + "-" + impleDay(da.getDate());
return weekFirstDay;
}
/* 得到当前周的最后一天(周日)的日期*/
public static String getWeekLastDate() {
Date da = new Date();
int dayOfWeek = da.getDay(); // 当前日期是星期几
long toWeekLastInMillis = (WeekSpan - dayOfWeek) * 24 * 60 * 60 * 1000; // 与该周最后一天相隔的毫秒数
da.setTime(da.getTime() + toWeekLastInMillis);
String weekLastDay = new Integer(da.getYear() + 1900).toString();
weekLastDay = weekLastDay + "-" + impleMonth(da.getMonth() + 1);
weekLastDay = weekLastDay + "-" + impleDay(da.getDate());
return weekLastDay;
}
/* 得到当前月的第一天的日期*/
public static String getMonthFirstDate() {
Date da = new Date();
long dayOfMonth = da.getDate(); // 当前日期是本月第几天
long fromMonthFirstInMillis = (dayOfMonth - 1) * 24 * 60 * 60 * 1000; // 与该月第一天相隔的毫秒数
da.setTime(da.getTime() - fromMonthFirstInMillis);
String MonthFirstDay = new Integer(da.getYear() + 1900).toString();
MonthFirstDay = MonthFirstDay + "-" + impleMonth(da.getMonth() + 1);
MonthFirstDay = MonthFirstDay + "-" + impleDay(da.getDate());
return MonthFirstDay;
}
/* 得到当前月的最后一天的日期*/
public static String getMonthLastDate() {
Date da = new Date();
long dayOfMonth = da.getDate(); // 当前日期是本月第几天
int monthSpan = 0;
if ( (da.getMonth() + 1) == 2) {
if (LeapYear(da.getYear() + 1900)) {
monthSpan = 29;
}
else {
monthSpan = month[da.getMonth()];
}
}
else {
monthSpan = month[da.getMonth()];
}
// System.out.println("the montheSpan: "+monthSpan);
long toMonthLastInMillis = (monthSpan - dayOfMonth) * 24 * 60 * 60 * 1000; // 与该月最后一天相隔的毫秒数
da.setTime(da.getTime() + toMonthLastInMillis);
String MonthLastDay = new Integer(da.getYear() + 1900).toString();
MonthLastDay = MonthLastDay + "-" + impleMonth(da.getMonth() + 1);
MonthLastDay = MonthLastDay + "-" + impleDay(da.getDate());
return MonthLastDay;
}
/* 得到当前的日期*/
public static String getCurrentDate() {
Date da = new Date();
String currentDay = new Integer(da.getYear() + 1900).toString();
currentDay = currentDay + "-" + impleMonth(da.getMonth() + 1);
currentDay = currentDay + "-" + impleDay(da.getDate());
return currentDay;
}
/**
* add by gm
*/
//按长度把字符串前补0
public static String strLen1(String s, int len) {
if (isNullStr(s)) {
s = "";
}
int strLen = s.length();
for (int i = 0; i < len - strLen; i++) {
s = "0" + s;
}
return s;
}
public static String strLen(String s, int len) {
if (isNullStr(s)) {
s = "";
}
if (s.length()==0){
return s;
}
for (int i = 0; i < len - s.length(); i++) {
s = "0" + s;
if (s.length() ==0) {
break;
}
}
return s;
}
//判断字符串是否为空
public static boolean isNullStr(String s) {
if (s == null || s.trim().length() <= 0) {
return true;
}
else {
return false;
}
}
//判断字符串数组是否为空
public static boolean isNullStr(String[] s) {
if ( (s == null) || (s.length <= 0)) {
return true;
}
else {
return false;
}
}
//返回日历的年字符串
public static String getYear(Calendar cal) {
return String.valueOf(cal.get(cal.YEAR));
}
//返回日历的月字符串(两位)
public static String getMonth(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.MONTH) + 1), 2);
}
//返回日历的日字符串(两位)
public static String getDay(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.DAY_OF_MONTH)), 2);
}
//返回日历的时字符串(两位)
public static String getHour(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.HOUR_OF_DAY)), 2);
}
//返回日历的分字符串(两位)
public static String getMinute(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.MINUTE)), 2);
}
//返回日历的秒字符串(两位)
public static String getSecond(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.SECOND)), 2);
}
//由"yyyy-mm-dd"型串转换为Integer型yyyymmdd
public static Integer getNumDate(String strDate){
String theDay=null;
if(strDate!=null&&strDate.length()>9){
theDay=strDate.substring(0,4)+strDate.substring(5,7)+strDate.substring(8,10);
return new Integer(theDay);
}
return null;
}
//返回日历的日期字符串(格式:"yyyy-mm-dd")
public static String getDateStr(Calendar cal) {
return getYear(cal) + "-" + getMonth(cal) + "-" + getDay(cal);
}
//返回日历的日期字符串(格式:"yyyymmdd")
public static String getDateStr2(Calendar cal) {
return getYear(cal) + getMonth(cal) + getDay(cal);
}
//返回日历的日期字符串(格式:"yyyy-mm-dd")
public static String getDateStr3(Integer date) {
String result="";
String dateStr=String.valueOf(date);
if (dateStr!=null && dateStr.length()==8){
result=dateStr.substring(0,4)+"-"+dateStr.substring(4,6)+"-"+dateStr.substring(6,8);
}
return result;
}
//返回日历的时间字符串(格式:"hh:ss")
public static String getTimeStr(Calendar cal) {
return getHour(cal) + ":" + getMinute(cal);
}
//返回日历的时间字符串(格式:"hh:ss:mm")
//add by LiuQinghua
public static String getTime2Str(Calendar cal) {
return getHour(cal) + ":" + getMinute(cal) + ":" + getSecond(cal);
}
//返回日历的日期时间字符串(格式:"yyyy-mm-dd hh:ss")
public static String getDate(Calendar cal) {
return getDateStr(cal) + " " + getTimeStr(cal);
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的年
public static int getYear(String s) {
if (s == null || s.length() < 10) {
return 1970;
}
return Integer.parseInt(s.substring(0, 4));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的月
public static int getMonth(String s) {
if (s == null || s.length() < 10) {
return 1;
}
return Integer.parseInt(s.substring(5, 7));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的日
public static int getDay(String s) {
if (s == null || s.length() < 10) {
return 1;
}
return Integer.parseInt(s.substring(8, 10));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的时
public static int getHour(String s) {
if (s == null || s.length() < 16) {
return 0;
}
return Integer.parseInt(s.substring(11, 13));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的分
public static int getMinute(String s) {
if (s == null || s.length() < 16) {
return 0;
}
return Integer.parseInt(s.substring(14, 16));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的秒
public static int getSecond(String s) {
if (s == null || s.length() < 18) {
return 0;
}
return Integer.parseInt(s.substring(16, 18));
}
//返回日期时间字符串对应的日历(格式:"yyyy-mm-dd hh:ss")
public static Calendar getCal(String s) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(s), getMonth(s), getDay(s), getHour(s), getMinute(s),
getSecond(s));
return cal;
}
//返回日期时间字符串对应的SQL日期(格式:"yyyy-mm-dd hh:ss")
public static java.sql.Date getSqlDate(String s) {
return java.sql.Date.valueOf(s);
}
//返回当天日期对应的SQL日期()
public static java.sql.Date getSqlDate() {
return java.sql.Date.valueOf(getNowDate());
}
//取当前日期时间的字符串,格式为"yyyy-mm-dd hh:ss"
public static String getNow() {
Calendar now = Calendar.getInstance();
return getDateStr(now) + " " + getTimeStr(now);
}
//取当前日期时间的字符串,格式为"yyyy-mm-dd hh:ss:mm"
//add by LiuQinghua
public static String getNow2() {
Calendar now = Calendar.getInstance();
return getDateStr(now) + " " + getTime2Str(now) ;
}
//取当前日期的整形串,格式为"yyyymmdd"
public static Integer getNumDate() {
Calendar now = Calendar.getInstance();
return new Integer(getDateStr2(now));
}
//取给定日期的整形串,格式为"yyyymmdd"
public static Integer getNumDate(Calendar cal) {
return new Integer(getDateStr2(cal));
}
//取当前日期的字符串,格式为"yyyy-mm-dd"
public static String getNowDate() {
Calendar now = Calendar.getInstance();
return getDateStr(now);
}
//取当前时间的字符串,格式为"hh:ss"
public static String getNowTime() {
Calendar now = Calendar.getInstance();
return getTimeStr(now);
}
//取当前时间的字符串
public static String getCurrentTimeMillisStr() {
return (new Long(System.currentTimeMillis()).toString());
}
//取当前时间的字符串
public static String changTimeMillisToStr(String time) {
long t = 0l;
try {
t = Long.parseLong(time);
}
catch (Exception e) {
return "";
}
Date date = new Date();
date.setTime(t);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return getDateStr(cal) + " " + getTimeStr(cal);
}
public static String changTimeMillisToStr(long time) {
String str = "";
try {
str = Long.toString(time);
}
catch (Exception e) {
}
return changTimeMillisToStr(str);
}
/**
* 格式化字符串为日期的函数.
*
* @param str 字符串.
* @param FORMat 转换格式如:"yyyy-MM-dd mm:ss"
* @return 字符串包含的日期.
*/
public static Date parseDate(String strDate, String FORMat) {
try {
if (strDate == null || strDate.equals(""))
return null;
SimpleDateFormat simpleDateFORMat = new SimpleDateFormat(FORMat);
return simpleDateFORMat.parse(strDate);
}
catch (Exception e) {
}
return new Date();
}
/**
* 格式化日期为字符串函数.
* @param date 日期.
* @param sFORMat 转换格式."yyyy-MM-dd mm:ss"
* @return 日期转化来的字符串.
*/
public static String FORMatDate(Date date, String FORMat) {
if (date == null)
return "";
SimpleDateFormat simpleDateFORMat = new SimpleDateFormat(FORMat);
return simpleDateFORMat.format(date);
}
/**
* 取得preNum天前的当前时间字符串函数.
* @param preNum 天数
* @return preNum天前的当前时间字符串,格式"yyyy-MM-dd hh:mm:ss".
*/
public static String getPreDateStr(int preNum){
String result=null;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-preNum);
result=getDateStr(cal) + " " + getTime2Str(cal) ;
return result;
}
public static void main(String[] args)
{
DateUtil DateUtil=new DateUtil();
System.out.println(DateUtil.getNowDate());
Integer integer=DateUtil.getNumDate(DateUtil.getNowDate());
System.out.println(integer.intValue());
}
}
* ZhouJianGuo
* Dec 25, 2007
* MSN:zhoujianguo_leo@hotmail.com
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author zhoujianguo
* Dec 25, 2007
* Tenwa
* DateUtil.java
*/
public class DateUtil {
public final static int WeekSpan = 7;
public static int month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/* 判断是否闰年*/
private static boolean LeapYear(int year) {
if ( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
//System.out.println(year+" is a leap year.");
return true;
}
else {
//System.out.println(year+" is not a leap year.");
return false;
}
}
/* 对个位数的月份之前补零 */
private static String impleMonth(int month) {
String monthStr = new Integer(month).toString();
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
return monthStr;
}
/* 对个位数的日子之前补零 */
private static String impleDay(int day) {
String dayStr = new Integer(day).toString();
if (dayStr.length() == 1) {
dayStr = "0" + dayStr;
}
return dayStr;
}
/* 得到当前周的第一天(周一)的日期*/
public static String getWeekFirstDate() {
Date da = new Date();
int dayOfWeek = da.getDay(); // 当前日期是星期几
long fromWeekFirstInMillis = (dayOfWeek - 1) * 24 * 60 * 60 * 1000; // 与该周第一天相隔的毫秒数
da.setTime(da.getTime() - fromWeekFirstInMillis);
String weekFirstDay = new Integer(da.getYear() + 1900).toString();
weekFirstDay = weekFirstDay + "-" + impleMonth(da.getMonth() + 1);
weekFirstDay = weekFirstDay + "-" + impleDay(da.getDate());
return weekFirstDay;
}
/* 得到当前周的最后一天(周日)的日期*/
public static String getWeekLastDate() {
Date da = new Date();
int dayOfWeek = da.getDay(); // 当前日期是星期几
long toWeekLastInMillis = (WeekSpan - dayOfWeek) * 24 * 60 * 60 * 1000; // 与该周最后一天相隔的毫秒数
da.setTime(da.getTime() + toWeekLastInMillis);
String weekLastDay = new Integer(da.getYear() + 1900).toString();
weekLastDay = weekLastDay + "-" + impleMonth(da.getMonth() + 1);
weekLastDay = weekLastDay + "-" + impleDay(da.getDate());
return weekLastDay;
}
/* 得到当前月的第一天的日期*/
public static String getMonthFirstDate() {
Date da = new Date();
long dayOfMonth = da.getDate(); // 当前日期是本月第几天
long fromMonthFirstInMillis = (dayOfMonth - 1) * 24 * 60 * 60 * 1000; // 与该月第一天相隔的毫秒数
da.setTime(da.getTime() - fromMonthFirstInMillis);
String MonthFirstDay = new Integer(da.getYear() + 1900).toString();
MonthFirstDay = MonthFirstDay + "-" + impleMonth(da.getMonth() + 1);
MonthFirstDay = MonthFirstDay + "-" + impleDay(da.getDate());
return MonthFirstDay;
}
/* 得到当前月的最后一天的日期*/
public static String getMonthLastDate() {
Date da = new Date();
long dayOfMonth = da.getDate(); // 当前日期是本月第几天
int monthSpan = 0;
if ( (da.getMonth() + 1) == 2) {
if (LeapYear(da.getYear() + 1900)) {
monthSpan = 29;
}
else {
monthSpan = month[da.getMonth()];
}
}
else {
monthSpan = month[da.getMonth()];
}
// System.out.println("the montheSpan: "+monthSpan);
long toMonthLastInMillis = (monthSpan - dayOfMonth) * 24 * 60 * 60 * 1000; // 与该月最后一天相隔的毫秒数
da.setTime(da.getTime() + toMonthLastInMillis);
String MonthLastDay = new Integer(da.getYear() + 1900).toString();
MonthLastDay = MonthLastDay + "-" + impleMonth(da.getMonth() + 1);
MonthLastDay = MonthLastDay + "-" + impleDay(da.getDate());
return MonthLastDay;
}
/* 得到当前的日期*/
public static String getCurrentDate() {
Date da = new Date();
String currentDay = new Integer(da.getYear() + 1900).toString();
currentDay = currentDay + "-" + impleMonth(da.getMonth() + 1);
currentDay = currentDay + "-" + impleDay(da.getDate());
return currentDay;
}
/**
* add by gm
*/
//按长度把字符串前补0
public static String strLen1(String s, int len) {
if (isNullStr(s)) {
s = "";
}
int strLen = s.length();
for (int i = 0; i < len - strLen; i++) {
s = "0" + s;
}
return s;
}
public static String strLen(String s, int len) {
if (isNullStr(s)) {
s = "";
}
if (s.length()==0){
return s;
}
for (int i = 0; i < len - s.length(); i++) {
s = "0" + s;
if (s.length() ==0) {
break;
}
}
return s;
}
//判断字符串是否为空
public static boolean isNullStr(String s) {
if (s == null || s.trim().length() <= 0) {
return true;
}
else {
return false;
}
}
//判断字符串数组是否为空
public static boolean isNullStr(String[] s) {
if ( (s == null) || (s.length <= 0)) {
return true;
}
else {
return false;
}
}
//返回日历的年字符串
public static String getYear(Calendar cal) {
return String.valueOf(cal.get(cal.YEAR));
}
//返回日历的月字符串(两位)
public static String getMonth(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.MONTH) + 1), 2);
}
//返回日历的日字符串(两位)
public static String getDay(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.DAY_OF_MONTH)), 2);
}
//返回日历的时字符串(两位)
public static String getHour(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.HOUR_OF_DAY)), 2);
}
//返回日历的分字符串(两位)
public static String getMinute(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.MINUTE)), 2);
}
//返回日历的秒字符串(两位)
public static String getSecond(Calendar cal) {
return strLen(String.valueOf(cal.get(cal.SECOND)), 2);
}
//由"yyyy-mm-dd"型串转换为Integer型yyyymmdd
public static Integer getNumDate(String strDate){
String theDay=null;
if(strDate!=null&&strDate.length()>9){
theDay=strDate.substring(0,4)+strDate.substring(5,7)+strDate.substring(8,10);
return new Integer(theDay);
}
return null;
}
//返回日历的日期字符串(格式:"yyyy-mm-dd")
public static String getDateStr(Calendar cal) {
return getYear(cal) + "-" + getMonth(cal) + "-" + getDay(cal);
}
//返回日历的日期字符串(格式:"yyyymmdd")
public static String getDateStr2(Calendar cal) {
return getYear(cal) + getMonth(cal) + getDay(cal);
}
//返回日历的日期字符串(格式:"yyyy-mm-dd")
public static String getDateStr3(Integer date) {
String result="";
String dateStr=String.valueOf(date);
if (dateStr!=null && dateStr.length()==8){
result=dateStr.substring(0,4)+"-"+dateStr.substring(4,6)+"-"+dateStr.substring(6,8);
}
return result;
}
//返回日历的时间字符串(格式:"hh:ss")
public static String getTimeStr(Calendar cal) {
return getHour(cal) + ":" + getMinute(cal);
}
//返回日历的时间字符串(格式:"hh:ss:mm")
//add by LiuQinghua
public static String getTime2Str(Calendar cal) {
return getHour(cal) + ":" + getMinute(cal) + ":" + getSecond(cal);
}
//返回日历的日期时间字符串(格式:"yyyy-mm-dd hh:ss")
public static String getDate(Calendar cal) {
return getDateStr(cal) + " " + getTimeStr(cal);
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的年
public static int getYear(String s) {
if (s == null || s.length() < 10) {
return 1970;
}
return Integer.parseInt(s.substring(0, 4));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的月
public static int getMonth(String s) {
if (s == null || s.length() < 10) {
return 1;
}
return Integer.parseInt(s.substring(5, 7));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的日
public static int getDay(String s) {
if (s == null || s.length() < 10) {
return 1;
}
return Integer.parseInt(s.substring(8, 10));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的时
public static int getHour(String s) {
if (s == null || s.length() < 16) {
return 0;
}
return Integer.parseInt(s.substring(11, 13));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的分
public static int getMinute(String s) {
if (s == null || s.length() < 16) {
return 0;
}
return Integer.parseInt(s.substring(14, 16));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的秒
public static int getSecond(String s) {
if (s == null || s.length() < 18) {
return 0;
}
return Integer.parseInt(s.substring(16, 18));
}
//返回日期时间字符串对应的日历(格式:"yyyy-mm-dd hh:ss")
public static Calendar getCal(String s) {
Calendar cal = Calendar.getInstance();
cal.set(getYear(s), getMonth(s), getDay(s), getHour(s), getMinute(s),
getSecond(s));
return cal;
}
//返回日期时间字符串对应的SQL日期(格式:"yyyy-mm-dd hh:ss")
public static java.sql.Date getSqlDate(String s) {
return java.sql.Date.valueOf(s);
}
//返回当天日期对应的SQL日期()
public static java.sql.Date getSqlDate() {
return java.sql.Date.valueOf(getNowDate());
}
//取当前日期时间的字符串,格式为"yyyy-mm-dd hh:ss"
public static String getNow() {
Calendar now = Calendar.getInstance();
return getDateStr(now) + " " + getTimeStr(now);
}
//取当前日期时间的字符串,格式为"yyyy-mm-dd hh:ss:mm"
//add by LiuQinghua
public static String getNow2() {
Calendar now = Calendar.getInstance();
return getDateStr(now) + " " + getTime2Str(now) ;
}
//取当前日期的整形串,格式为"yyyymmdd"
public static Integer getNumDate() {
Calendar now = Calendar.getInstance();
return new Integer(getDateStr2(now));
}
//取给定日期的整形串,格式为"yyyymmdd"
public static Integer getNumDate(Calendar cal) {
return new Integer(getDateStr2(cal));
}
//取当前日期的字符串,格式为"yyyy-mm-dd"
public static String getNowDate() {
Calendar now = Calendar.getInstance();
return getDateStr(now);
}
//取当前时间的字符串,格式为"hh:ss"
public static String getNowTime() {
Calendar now = Calendar.getInstance();
return getTimeStr(now);
}
//取当前时间的字符串
public static String getCurrentTimeMillisStr() {
return (new Long(System.currentTimeMillis()).toString());
}
//取当前时间的字符串
public static String changTimeMillisToStr(String time) {
long t = 0l;
try {
t = Long.parseLong(time);
}
catch (Exception e) {
return "";
}
Date date = new Date();
date.setTime(t);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return getDateStr(cal) + " " + getTimeStr(cal);
}
public static String changTimeMillisToStr(long time) {
String str = "";
try {
str = Long.toString(time);
}
catch (Exception e) {
}
return changTimeMillisToStr(str);
}
/**
* 格式化字符串为日期的函数.
*
* @param str 字符串.
* @param FORMat 转换格式如:"yyyy-MM-dd mm:ss"
* @return 字符串包含的日期.
*/
public static Date parseDate(String strDate, String FORMat) {
try {
if (strDate == null || strDate.equals(""))
return null;
SimpleDateFormat simpleDateFORMat = new SimpleDateFormat(FORMat);
return simpleDateFORMat.parse(strDate);
}
catch (Exception e) {
}
return new Date();
}
/**
* 格式化日期为字符串函数.
* @param date 日期.
* @param sFORMat 转换格式."yyyy-MM-dd mm:ss"
* @return 日期转化来的字符串.
*/
public static String FORMatDate(Date date, String FORMat) {
if (date == null)
return "";
SimpleDateFormat simpleDateFORMat = new SimpleDateFormat(FORMat);
return simpleDateFORMat.format(date);
}
/**
* 取得preNum天前的当前时间字符串函数.
* @param preNum 天数
* @return preNum天前的当前时间字符串,格式"yyyy-MM-dd hh:mm:ss".
*/
public static String getPreDateStr(int preNum){
String result=null;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-preNum);
result=getDateStr(cal) + " " + getTime2Str(cal) ;
return result;
}
public static void main(String[] args)
{
DateUtil DateUtil=new DateUtil();
System.out.println(DateUtil.getNowDate());
Integer integer=DateUtil.getNumDate(DateUtil.getNowDate());
System.out.println(integer.intValue());
}
}