package com.mytripod.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Mytripod
 * @create 2018-09-30 21:25
 */
public class DateUtil {
    public static String DatePattern = "yyyy-MM-dd";

    public static String DateTimePattern = "yyyy-MM-dd HH:mm";

    public static Date parseDate(String source) {
        return parse(source, DatePattern);
    }

    public static Date parseDateTime(String source) {
        if(source.trim().length() == 10) source = source + " 00:00";
        return parse(source, DateTimePattern);
    }

    public static Date parse(String source, String patten) {
        if (StringUtil.isEmpty(source))
            return null;
        DateFormat format;
        try {
            format = new SimpleDateFormat(patten);
        } catch (Exception e) {
            format = new SimpleDateFormat(DateTimePattern);
        }
        Date date;
        try {
            date = format.parse(source);
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    public static String formatDate(Date date) {
        return format(date, DatePattern);
    }

    public static String formatDateTime(Date date) {
        return format(date, DateTimePattern);
    }

    public static String format(Date date, String patten) {
        if (date == null)
            return "";
        DateFormat format;
        try {
            format = new SimpleDateFormat(patten);
        } catch (Exception e) {
            format = new SimpleDateFormat(DateTimePattern);
        }
        return format.format(date);
    }

    public static String getFormatBeforeOrAfterDate(int offset) {
        return getFormatBeforeOrAfterDay(offset, DatePattern);
    }

    public static String getFormatBeforeOrAfterDateTime(int offset) {
        return getFormatBeforeOrAfterDay(offset, DateTimePattern);
    }

    public static String getFormatBeforeOrAfterDay(int offset, String patten) {
        return format(getBeforeOrAfterDay(offset), patten);
    }

    public static Date getBeforeOrAfterDay(int offset) {
        if (offset == 0)
            return new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, offset);
        return c.getTime();
    }

    public static int getYear() {
        return getYear(new Date());
    }

    public static int getMonth() {
        return getMonth(new Date());
    }

    public static int getDayOfWeek() {
        return getDayOfWeek(new Date());
    }

    public static int getDayOfMonth() {
        return getDayOfMonth(new Date());
    }

    public static int getDayOfYear() {
        return getDayOfYear(new Date());
    }

    public static int getYear(Date date) {
        return get(date, Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        return get(date, Calendar.MONTH) + 1;
    }

    public static int getDayOfWeek(Date date) {
        int day = get(date, Calendar.DAY_OF_WEEK);
        return day == 0 ? 7 : get(date, Calendar.DAY_OF_WEEK) - 1;
    }

    public static int getDayOfMonth(Date date) {
        return get(date, Calendar.DAY_OF_MONTH);
    }

    public static int getDayOfYear(Date date) {
        return get(date, Calendar.DAY_OF_YEAR);
    }

    private static Calendar getCalendar(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null)
            c.setTime(date);
        return c;
    }

    private static int get(Date date, int field) {
        Calendar c = getCalendar(date);
        return c.get(field);
    }

    public static boolean compareDateTime(String s1, String s2) {
        return compare(parseDateTime(s1), parseDateTime(s2));
    }

    public static boolean compareDateTime(String s, Date date) {
        return compare(parseDateTime(s), date);
    }

    public static boolean compareDateTime(Date date, String s) {
        return compare(date, parseDateTime(s));
    }

    public static boolean compareDate(String s1, String s2) {
        return compare(parseDate(s1), parseDate(s2));
    }

    public static boolean compareDate(String s, Date date) {
        return compare(parseDate(s), date);
    }

    public static boolean compareDate(Date date, String s) {
        return compare(date, parseDate(s));
    }

    public static boolean compare(String s1, String patten1, String s2,
                                  String patten2) {
        return compare(parse(s1, patten1), parse(s2, patten2));
    }

    public static boolean compare(String s, String patten, Date date) {
        return compare(parse(s, patten), date);
    }

    public static boolean compare(Date date, String s, String patten) {
        return compare(date, parse(s, patten));
    }

    public static boolean compare(Date d1, Date d2) {
        if (d1 == null || d2 == null)
            throw new IllegalArgumentException();
        return d1.after(d2);
    }

    //获取两个日期的间隔天数
    public static long getDiffDays (String strdate1,String strdate2){
        long diff = 0;
        try{
            Date date1 = parseDate(strdate1);
            Date date2 = parseDate(strdate2);
            diff = date2.getTime() - date1.getTime();
            diff = diff/1000/60/60/24;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return diff;
    }

    //获取结算周期开始日期:xxxx-xx-26
    public static String getFromDate(String date){
        return getFromDate(parseDate(date));
    }

    public static String getFromDate(Date date){
        String fromDate = "";
        int year = getYear(date);
        int month = getMonth(date);
        int day = getDayOfMonth(date);
        if (day <= 25) {
            if(month == 1){
                month =12;
                year = year-1;
            }
            else month = month-1;
        }
        if(month < 10)
            fromDate = year + "-0" + month + "-" + "26";
        else
            fromDate = year + "-" + month + "-" + "26";

        return fromDate;
    }

    //获取结算周期结束日期xxxx-xx-01
    public static String getEndDate(Date date){
        String monthOfThis = getMonthOfThis(formatDate(date));
        String endDate = monthOfThis.substring(0, 8)+"25";
        return endDate;
    }

    public static String getMonthOfThis(String date){
        String monthOfThis = "";
        Date date_ = parseDate(date);
        int year = getYear(date_);
        int month = getMonth(date_);
        int day = getDayOfMonth(date_);
        if (day > 25) {
            if(month == 12){
                month =1;
                year = year + 1;
            }
            else month = month+1;
        }
        if(month < 10)
            monthOfThis = year + "-0" + month + "-" + "01";
        else
            monthOfThis = year + "-" + month + "-" + "01";

        return monthOfThis;
    }

    /**
     * 获得指定日期的后一天
     * @param date
     * @return
     */
    public static String getNextDay(String date){
        Calendar c = Calendar.getInstance();
        Date date_ = parseDate(date);
        c.setTime(date_);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);
        return formatDate(c.getTime());
    }

    /**
     * 获得指定日期的前一天
     * @param date
     * @return
     */
    public static String getlastDay(String date){
        Calendar c = Calendar.getInstance();
        Date date_ = parseDate(date);
        c.setTime(date_);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-1);
        return formatDate(c.getTime());
    }

    /**
     * 获得指定日期的前一个月
     * @param date
     * @return
     */
    public static String getlastMonth(String date){
        Calendar c = Calendar.getInstance();
        Date date_ = parseDate(date);
        c.setTime(date_);
        c.add(Calendar.MONTH, -1);

        return formatDate(c.getTime());
    }

    /**
     * 获得某日期相差指定天数的日期
     * @param date
     * @return
     */
    public static String getDateByN(String date,int n){
        Calendar c = Calendar.getInstance();
        Date date_ = parseDate(date);
        c.setTime(date_);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+n);
        return formatDate(c.getTime());
    }

    public static void main(String arg[]){
        String dateOfBegin = "2018-09-28";
        System.out.println(getMonthOfThis(dateOfBegin));
       /* String today = DateUtil.formatDate(new Date());
        System.out.println(DateUtil.compareDate(today,dateOfBegin));
        System.out.println(getlastDay(formatDate(new Date())));
        System.out.println(getlastMonth(formatDate(new Date())));*/
    }

    /**
     * 获得指定日期的前一周
     * @param date
     * @return
     */
    public static String getlastWeek(String date){
        Calendar c = Calendar.getInstance();
        Date date_ = parseDate(date);
        c.setTime(date_);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-7);
        return formatDate(c.getTime());
    }
}

Java中Date类型的工具类的更多相关文章

  1. java中excel导入\导出工具类

    1.导入工具 package com.linrain.jcs.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import ...

  2. java中的Arrays这个工具类你真的会用吗

    Java源码系列三-工具类Arrays ​ 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...

  3. java中定义一个CloneUtil 工具类

    其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...

  4. Java中Date类型详解

    一.Date类型的初始化 1. Date(int year, int month, int date); 直接写入年份是得不到正确的结果的. 因为java中Date是从1900年开始算的,所以前面的第 ...

  5. Java中Date类型如何向前向后滚动时间,( 附工具类)

    Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...

  6. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

  7. Java中Date类型与String 类型之间的互相转换

    Java中String类型和Date类型之间的转换 我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需 ...

  8. 在JAVA中自定义连接数据库的工具类

    为什么要自定义数据库连接的工具类: 在开发中,我们在对数据库进行操作时,必须要先获取数据库的连接,在上一篇随笔中提到的获取数据库连接的步骤为: 1.定义好4个参数并赋值 2.加载驱动类 3.获取数据库 ...

  9. Java中的集合Collections工具类(六)

    操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...

随机推荐

  1. bzoj 4785: [Zjoi2017]树状数组【树套树】

    参考:https://www.cnblogs.com/ljh2000-jump/p/6686960.html 由于操作反过来了,所以显然树状数组维护后缀和,所以本来想查询(1,r)-(1,l-1),现 ...

  2. 洛谷P2082 区间覆盖(加强版)(珂朵莉树)

    传送门 虽然是黄题而且还是一波离散就能解决的东西 然而珂朵莉树还是很好用 相当于一开始区间全为0,然后每一次区间赋值,问最后总权值 珂朵莉树搞一搞就好了 //minamoto #include< ...

  3. django views视图函数

    Django views.py视图文件 一. 创建views.py文件,在工程文件夹根目录创建views.py视图文件,其实任意文件名都可以,使用views是为了遵循传统. 注:所有的views函数都 ...

  4. ROS学习笔记五:创建和使用ROS msg和srv

    1 msg和srv简介 1.1 msg文件 msg文件就是一个简单的text文件,其中每行有一个类型和名称,可用的类型如下: int8, int16, int32, int64 (plus uint* ...

  5. QT如何发布程序

    QT如何发布程序转载 http://blog.csdn.net/iw1210/article/details/51253458

  6. ORA-00445: Background Process "xxxx" Did Not Start After 120 Seconds

    Recent linux kernels have a feature called Address Space Layout Randomization (ASLR).ASLR  is a feat ...

  7. C#_JDBC连接数据库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. java课程设计全程实录——第2天

    [反思] 今天主要完成JDBC数据的连接,查阅了大量博客和书籍,繁琐而细碎.但所幸还是连上了. [日常烦心事] 下午准备用idea连测试连接的,结果电脑跑不动....CPU一度100%居高不下,ide ...

  9. [备忘]Notification的实用

    Intent resultIntent = null; if (!TextUtils.isEmpty(tid)){ resultIntent = new Intent("com.shijie ...

  10. VS2017 移动开发(Android and IOS) 序

    序 公司原因,要求用C#开发移动端app,老板觉得用现在会的C#做会比较快... 从零开始,折腾一个多星期,重装系统三遍(强迫症),其它各种折腾,终于手机运行上了第一个APP,看看就好... 不得不吐 ...