首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

时间date工具种

2012-12-23 
时间date工具类public class DateTool {private DateTool() {}public static Date getNow() {return Calen

时间date工具类
public class DateTool {

private DateTool() {
}

public static Date getNow() {
return Calendar.getInstance().getTime();
}

public static String getDate() {
return getDateTime("yyyy-MM-dd");
}

public static String getYM() {
return getDateTime("yyyy-MM");
}

public static String getDateTime() {
return getDateTime("yyyy-MM-dd HH:mm:ss");
}

//获得当前月的上一
public static String getLastMonth(String str,String format) throws ParseException{
SimpleDateFormat df  =  new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
Date date = df.parse(str);
calendar.setTime(date);
        calendar.add(Calendar.MONTH,-1);  
        return df.format(calendar.getTime());
}
    //获得当前月的前两个月
public static String getLastMonth1(String str,String format) throws ParseException{
SimpleDateFormat df  =  new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(df.parse(str));  
        calendar.add(Calendar.MONTH,-2);  
        return df.format(calendar.getTime());
}
//获得当前月的下一个月
public static String getLastMonth2(String str,String format) throws ParseException{
SimpleDateFormat df  =  new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(df.parse(str));  
        calendar.add(Calendar.MONTH,+1);  
        return df.format(calendar.getTime());
}

// 获得当前日期前
public static String getPreDate() {
Date cur = Calendar.getInstance().getTime();
Date pre = new Date(cur.getTime() - 24 * 60 * 60 * 1000);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(pre);
}

// 获得当前月的第一
public static String getFirstDayOfMonth() {
Calendar c = Calendar.getInstance();
Calendar calfirst = Calendar.getInstance();
int now = c.get(c.DAY_OF_MONTH);
calfirst.add(calfirst.DATE, 1 - now);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(calfirst.getTime());
}

public static String getDateTime(String pattern) {
Date datetime = Calendar.getInstance().getTime();
return getDateTime(datetime, pattern);
}

public static String getDateTime(Date date, String pattern) {
if (pattern == null || "".equals(pattern))
pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}

public static int getCurrentYear() {
return Calendar.getInstance().get(1);
}

public static int getCurrentMonth() {
return Calendar.getInstance().get(2) + 1;
}

public static int getCurrentDay() {
return Calendar.getInstance().get(5);
}

public static Date addDays(int days) {
return add(getNow(), days, 5);
}

public static Date addDays(Date date, int days) {
return add(date, days, 5);
}

public static Date addMonths(int months) {
return add(getNow(), months, 2);
}

public static Date addMonths(Date date, int months) {
return add(date, months, 2);
}

// 2007-11 to 2007
public static int getYear(String date) {
String[] str = date.split("-");
return Integer.parseInt(str[0]);
}

// 2007-11 to 11
public static String getMonth(String date) {
String[] str = date.split("-");
return str[1];
}

// 2007-11 to 2007-11-30
public static String getDay(String date) {
if (date == null || date.equals("")) {
return null;
}
try {
String[] str = date.split("-");
int month = Integer.parseInt(str[1]);
if (month == 2) {
return "-28";
} else if (month == 1 || month == 3 || month == 5 || month == 7
|| month == 8 || month == 10 || month == 12) {
return "-31";
} else {
return "-30";
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

// 2007-01-01 to >=2007-01-01 00:00:00 and <=2007-01-01 23:59:59
public static String getDayAll(String date, String flag) {
if (flag == null || flag.equals(""))
return null;

try {
if (flag.equals("start")) {
return date + " 00:00:00";
} else if (flag.equals("end")) {
return date + " 23:59:59";
}
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* 获取前一
*
* @param curYear
* @return
*/
public static String getPreYear(String curYear) {
int curY = Integer.parseInt(curYear);
int preY = curY - 1;
return preY + "";
}

private static Date add(Date date, int amount, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
return calendar.getTime();
}

public static long diffDays(Date one, Date two) {
return (one.getTime() - two.getTime()) / 0x5265c00L;
}

public static int diffMonths(Date one, Date two) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(one);
int yearOne = calendar.get(1);
int monthOne = calendar.get(2);
calendar.setTime(two);
int yearTwo = calendar.get(1);
int monthTwo = calendar.get(2);
return (yearOne - yearTwo) * 12 + (monthOne - monthTwo);
}

public static Date parse(String datestr, String pattern) {
if (datestr == null || "".equals(datestr))
return null;
Date date = null;
String p = pattern;
if (pattern == null || "".equals(pattern))
p = "yyyy-MM-dd";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(p);
date = dateFormat.parse(datestr);
} catch (ParseException parseexception) {
}
return date;
}

public static String format(Date date, String pattern) {
String p;
p = pattern;
if (pattern == null || "".equals(pattern))
p = "yyyy-MM-dd";
SimpleDateFormat dateFormat = new SimpleDateFormat(p);
try {
return dateFormat.format(date);
} catch (Exception e) {
return "";
}
}

public static Date getMonthLastDay() {
return getMonthLastDay(getNow());
}

public static Date getMonthLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(1), calendar.get(2) + 1, 1);
calendar.add(5, -1);
return calendar.getTime();
}

public static void main(String args[]) {
String test = "2003-1-31";
try {
Date date = parse(test, "");
System.out
.println("\u5F97\u5230\u5F53\u524D\u65E5\u671F \uFF0D getDate():"
+ getDate());
System.out
.println("\u5F97\u5230\u5F53\u524D\u65E5\u671F\u65F6\u95F4 \uFF0D getDateTime():"
+ getDateTime());
System.out
.println("\u5F97\u5230\u5F53\u524D\u5E74\u4EFD \uFF0D getCurrentYear():"
+ getCurrentYear());
System.out
.println("\u5F97\u5230\u5F53\u524D\u6708\u4EFD \uFF0D getCurrentMonth():"
+ getCurrentMonth());
System.out
.println("\u5F97\u5230\u5F53\u524D\u65E5\u5B50 \uFF0D getCurrentDay():"
+ getCurrentDay());
System.out.println("\u89E3\u6790 \uFF0D parse(" + test + "):"
+ getDateTime(date, "yyyy-MM-dd"));
System.out.println("\u81EA\u589E\u6708\u4EFD \uFF0D addMonths(3):"
+ getDateTime(addMonths(3), "yyyy-MM-dd"));
System.out.println("\u589E\u52A0\u6708\u4EFD \uFF0D addMonths("
+ test + ",3):"
+ getDateTime(addMonths(date, 3), "yyyy-MM-dd"));
System.out.println("\u589E\u52A0\u65E5\u671F \uFF0D addDays("
+ test + ",3):"
+ getDateTime(addDays(date, 3), "yyyy-MM-dd"));
System.out.println("\u81EA\u589E\u65E5\u671F \uFF0D addDays(3):"
+ getDateTime(addDays(3), "yyyy-MM-dd"));
System.out.println("\u6BD4\u8F83\u65E5\u671F \uFF0D diffDays():"
+ diffDays(getNow(), date));
System.out.println("\u6BD4\u8F83\u6708\u4EFD \uFF0D diffMonths():"
+ diffMonths(getNow(), date));
System.out.println("\u5F97\u5230" + test
+ "\u6240\u5728\u6708\u4EFD\u7684\u6700\u540E\u4E00\u5929:"
+ getDateTime(getMonthLastDay(date), "yyyy-MM-dd"));
System.out.println(getPreDate());

} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}

public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

public static final int MAXYEAR = 2030; // 下拉列表年度选项的最大

public static final int MINYEAR = 1980; // 下拉列表年度选项的最小
}

热点排行