`
bianlong
  • 浏览: 5456 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

时间date工具类

    博客分类:
  • Java
阅读更多
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; // 下拉列表年度选项的最小
}
分享到:
评论

相关推荐

    java的Date工具类

    这是本人自己整理的java中的Date工具类,包含了常用的时间格式化方法和转换方法;在工作中使用起来还是蛮方便的;希望对大家有帮助!

    Date日期操作工具类

    在Java编程中,Date类是处理日期和时间的基础类,但在实际开发中,由于Date类本身的API设计并不十分友好,通常我们会使用工具类来简化日期的处理工作。本主题聚焦于一个名为"Date日期操作工具类"的实用工具,它提供...

    String和Date工具类

    本篇文章将详细讲解`String`和`Date`工具类中涉及的知识点,以及如何在实际开发中有效地运用它们。 首先,我们来看`String`工具类。在Java中,`String`对象是不可变的,这意味着一旦创建,就不能改变其内容。因此,...

    Date时间操作工具类

    Date时间操作工具类 js: 1.一个日期时间加上分钟数,得到一个新的日期时间 2.判断当前时间是否在时间段之内 3.根据默认的格式格式化时间 4.根据指定的格式格式化时间 5.改变月份 6.改变天数 7.计算天数间隔 8.Date转...

    Java - DateUtil 日期时间转换工具类

    内容概要:日期时间转换工具类,包括基本的Date类型,String类型,TimeStamp类型,LocalDateTime类型,LocalDate类型之间的互相转换,还提供了许多与时间获取,时间计算有关的方法,如:获取指定日期几天后的日期;...

    时间格式化工具类

    "时间格式化工具类"通常是指自定义的类,用于封装Java标准库中的`java.time`或`java.text.SimpleDateFormat`等类的功能,以提供更方便、更灵活的时间处理方法。下面我们将深入探讨这个主题。 首先,`java.time`包是...

    基于Java语言编写的Spring Boot时间工具类方法封装

    基于Java语言编写的Spring Boot时间工具类方法封装,主要方法如下:根据日期获取该日期内24小时的整点时刻、获取当月的 天数、根据 年、月 获取对应的月份 的 天数、根据指定日期得获取对应月份的所有日期的每日结束...

    时间工具类 DateUtils

    在Java编程语言中,时间工具类是用于处理日期和时间操作的重要工具,它们极大地简化了开发者对日期和时间的操作。本篇文章将详细讲解基于提供的"时间工具类 DateUtils"的知识点,包括DateUtils的主要功能、如何使用...

    一个long型与Date型时间转换工具

    这个“一个long型与Date型时间转换工具”正是为了简化这类操作而设计的。Long类型在Java中通常用来存储时间戳,即从1970年1月1日(UTC)开始到现在的毫秒数。Date对象则用于表示特定的瞬间,精确到毫秒。转换工具的...

    java操作日期时间工具类

    Java提供了多种工具类来帮助开发者进行日期时间的操作,其中最常用的是`java.util.Date`、`java.util.Calendar`以及`java.time`包中的类。本篇将重点介绍Java操作日期时间的工具类,特别是通过`DateUtil.java`这个...

    java 使用Date类获取系统的当前时间

    总的来说,`java.util.Date` 类在获取和表示当前系统时间上是一个基础且实用的工具,虽然在新的Java版本中有了更好的替代品,但理解其工作原理对于理解Java的日期和时间处理至关重要。在实际编程中,根据项目需求和...

    操作日期时间的工具类,封装的比较完善

    java写的,操作日期时间的工具类,封装的比较完善,可以获取最小时间, 获取最大时间,获取今天最小时间,获取今天最大时间,获取明天的最小时间,获取明天的最大时间,获取昨天的最小时间,获取昨天的最大时间,...

    java操作时间工具类

    Java操作时间工具类是开发者日常工作中经常会用到的模块,特别是在处理日期和时间相关的业务逻辑时。本工具类主要是为了方便地对日期和时间进行格式化、比较、计算等操作,提高开发效率。下面我们将详细探讨Java中...

    时间转换工具类

    时间转换工具类/将时间转换为时间戳/将时间戳转换为时间/计算时间天数/String日期转Date/Date日期转Date/获取两个时间的时间查 如1天2小时30分钟

    Java日期工具类,Java时间工具类,Java时间格式化

    本篇将围绕Java中的日期工具类和时间工具类展开讨论,同时会涉及到日期时间的格式化。 首先,Java 8之前,我们主要依赖`java.util.Date`和`java.text.SimpleDateFormat`这两个类来处理日期和时间。`Date`类用于表示...

    date时间处理工具类

    处理date添加或减少天数 处理date添加或减少月份 处理date添加或减少年份

    google Guava集合工具类(超实用)

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...

    java时间工具类,基本上全面的包括常用的关于时间的处理

    Java时间工具类是Java开发中不可或缺的一部分,它们提供了一系列方便的方法来处理日期和时间,使得开发者可以更加高效地进行时间相关的操作。在这个简单的DateUtil.java工具类中,我们可以期待找到一些基本但实用的...

    时间日期工具类(包含java8新特性).zip

    `时间日期工具类`是提升开发效率的关键,它们提供了对日期和时间进行格式化、比较、计算等操作的便捷方法。在这个"时间日期工具类(包含java8新特性).zip"压缩包中,我们有两个文件:DateUtil.java和DateUtils.java...

    一个简单的时间格式化工具类

    标题中的“一个简单的时间格式化工具类”指的是一个Java编程中的实用工具类,它主要用于处理日期和时间的格式化工作。在Java开发中,日期和时间的格式化是常见的需求,例如将日期从一种格式转换为另一种格式,或者将...

Global site tag (gtag.js) - Google Analytics