当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

时间date工具类

发表于: 2012-05-07   作者:bianlong   来源:转载   浏览次数:
摘要: public class DateTool { private DateTool() { } public static Date getNow() { return Calendar.getInstance().getTime(); } public static String getDate() { return getDateTime("yy
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; // 下拉列表年度选项的最小
}

时间date工具类

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1、使用 SimpleDateFormat 来对日期时间进行格式化,如可以将日期转换为指定格式的文本,也可将文本
一、题目简介 1、表示时间的数据域hour、minute和second: private int hour; private int minute;
图片是后来添加上去的 遇到的问题: 今天,搞了一个属于自己的日历,碰到一个比较纠结的问题,就是
关于时间,在JavaDoc中谈论比较多文字的是UTC、UT、GMT、TimeZone等 下面是科学的对它们的简单解释。
(转载):http://www.educity.cn/develop/683799.html 这是PHP性能优化系列第二期,如何使用PEAR工
本章主要介绍Date类,并通过示例学习如何使用它。最后,讲解一下UTC、GMT和时区的关系。 Date 介绍
这个控件关键是它能支持在mobile中显示。iPhone和android中显示可能略有差别。 下图是在android中的
需求 今天做日志分析时,由于日志是按天分割的,我需要遍历30天的日志,如果获得类似20130101-20130
这个控件关键是它能支持在mobile中显示。iPhone和android中显示可能略有差别。 下图是在android中的
这个控件关键是它能支持在mobile中显示。iPhone和android中显示可能略有差别。 下图是在android中的
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号