• java基础篇 -- 常用的日期加减和日期格式化工具类


      平时我们遇到日期的加减,感觉是相当麻烦的,以下是常用的日志加减的方法,包括日的加减、月的加减等,也包括了一些常用的日期格式化,这样在我们以后碰到日期加减的时候会省去很多麻烦,欢迎大神指正和吐槽:

      1   package bp.util;
      2 
      3 import java.text.ParseException;
      4 import java.text.SimpleDateFormat;
      5 import java.util.Calendar;
      6 import java.util.Date;
      7 
      8 public final class DateUtils
      9 {
     10     /**
     11      * 英文简写(默认)如:2010-12-01
     12      */
     13     public static String FORMAT_SHORT = "yyyy-MM-dd";
     14     /**
     15      * 英文简写(默认)如:2010-12-01
     16      */
     17     public static String FORMAT_DIY = "yyyy/MM/dd";
     18      
     19     /**
     20      * 英文全称  如:2010-12-01 23:15:06
     21      */
     22     public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
     23      
     24     /**
     25      * 精确到毫秒的完整时间    如:yyyy-MM-dd HH:mm:ss.S
     26      */
     27     public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
     28      
     29     /**
     30      * 中文简写  如:2010年12月01日
     31      */
     32     public static String FORMAT_SHORT_CN = "yyyy年MM月dd";
     33      
     34     /**
     35      * 中文全称  如:2010年12月01日  23时15分06秒
     36      */
     37     public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
     38      
     39     /**
     40      * 精确到毫秒的完整中文时间
     41      */
     42     public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";
     43  
     44     /**
     45      * 获得默认的 date pattern
     46      */
     47     public static String getDatePattern() {
     48         return FORMAT_LONG;
     49     }
     50     /**
     51      * 获得默认的 date
     52      */
     53     public static String getDatePatt() {
     54         return FORMAT_DIY;
     55     }
     56     public static String getDateShot() {
     57         return FORMAT_SHORT;
     58     }
     59     /**
     60      * 根据预设格式返回当前日期
     61      * @return
     62      */
     63     public static String getNow() {
     64         return format(new Date());
     65     }
     66      
     67     public static Date getDate(){
     68         return new Date();
     69     }
     70     /**
     71      * 根据用户格式返回当前日期
     72      * @param format
     73      * @return
     74      */
     75     public static String getNow(String format) {
     76         return format(new Date(), format);
     77     }
     78  
     79      
     80     /**
     81      * 使用预设格式格式化日期
     82      * @param date
     83      * @return
     84      */
     85     public static String format(Date date) {
     86         return format(date, getDatePattern());
     87     }
     88     /**
     89      * 使用预设格式格式化日期
     90      * @param date
     91      * @return
     92      */
     93     public static String format1(Date date) {
     94         return format(date, getDatePatt());
     95     }
     96     
     97     public static String format2(Date date) {
     98             return format(date, getDateShot());
     99         }
    100     /**
    101      * 使用用户格式格式化日期
    102      * @param date 日期
    103      * @param pattern 日期格式
    104      * @return
    105      */
    106     public static String format(Date date, String pattern) {
    107         String returnValue = "";
    108         if (date != null) {
    109             SimpleDateFormat df = new SimpleDateFormat(pattern);
    110             returnValue = df.format(date);
    111         }
    112         return (returnValue);
    113     }
    114  
    115     /**
    116      * 使用预设格式提取字符串日期
    117      * @param strDate 日期字符串
    118      * @return
    119      */
    120     public static Date parse(String strDate) {
    121         return parse(strDate, getDatePattern());
    122     }
    123  
    124     public static Date parseShot(String strDate) {
    125             return parse(strDate, getDateShot());
    126     }
    127     
    128     /**
    129      * 使用用户格式提取字符串日期
    130      * @param strDate 日期字符串
    131      * @param pattern 日期格式
    132      * @return
    133      */
    134     public static Date parse(String strDate, String pattern) {
    135         SimpleDateFormat df = new SimpleDateFormat(pattern);
    136         try {
    137             return df.parse(strDate);
    138         } catch (ParseException e) {
    139             e.printStackTrace();
    140             return null;
    141         }
    142     }
    143  
    144     /**
    145      * 在日期上增加数个整月
    146      * @param date 日期
    147      * @param n 要增加的月数
    148      * @return
    149      */
    150     public static Date addMonth(Date date, int n) {
    151         Calendar cal = Calendar.getInstance();
    152         cal.setTime(date);
    153         cal.add(Calendar.MONTH, n);
    154         return cal.getTime();
    155     }
    156  
    157     /**
    158      * 在日期上增加天数
    159      * @param date 日期
    160      * @param n 要增加的天数
    161      * @return
    162      */
    163     public static Date addDay(Date date, int n) {
    164         Calendar cal = Calendar.getInstance();
    165         cal.setTime(date);
    166         cal.add(Calendar.DATE, n);
    167         return cal.getTime();
    168     }
    169  
    170     /**
    171      * 获取时间戳
    172      */
    173     public static String getTimeString() {
    174         SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
    175         Calendar calendar = Calendar.getInstance();
    176         return df.format(calendar.getTime());
    177     }
    178  
    179     /**
    180      * 获取日期年份
    181      * @param date 日期
    182      * @return
    183      */
    184     public static String getYear(Date date) {
    185         return format(date).substring(0, 4);
    186     }
    187      
    188     /**
    189      * 按默认格式的字符串距离今天的天数
    190      * @param date 日期字符串
    191      * @return
    192      */
    193     public static int countDays (String date) {
    194         long t = Calendar.getInstance().getTime().getTime();
    195         Calendar c = Calendar.getInstance();
    196         c.setTime(parse(date));
    197         long t1 = c.getTime().getTime();
    198         return (int)(t/1000 - t1/1000)/3600/24;
    199     }
    200      
    201     /**
    202      * 按用户格式字符串距离今天的天数
    203      * @param date 日期字符串
    204      * @param format 日期格式
    205      * @return
    206      */
    207     public static int countDays (String date, String format) {
    208         long t = Calendar.getInstance().getTime().getTime();
    209         Calendar c = Calendar.getInstance();
    210         c.setTime(parse(date, format));
    211         long t1 = c.getTime().getTime();
    212         return (int)(t/1000 - t1/1000)/3600/24;
    213     }
    214     
    215     /**
    216      * 
    217     * @Title: getBackTime 
    218     * @Description: TODO(这里用一句话描述这个方法的作用) 
    219     * @param @param timestr
    220     * @param @return    设定文件 
    221     * @return Date    返回类型 
    222     * @throws
    223      */
    224     public static Date getBackTime(String timestr) {
    225         Date curTime = new Date();
    226         Date startTime = DateUtils.addMonth(curTime, -12);
    227         if (timestr.equals("ONEYEAE")) {
    228         } else if (timestr.equals("TWOYEAR")) {
    229             startTime = DateUtils.addMonth(curTime, -12 * 2);
    230         } else if (timestr.equals("ALL")) {
    231             startTime = null;
    232         }
    233         return startTime;
    234     }  
    235 }
    Success is getting what you want, happiness is wanting what you get.
  • 相关阅读:
    Vue.js监听事件
    Vue.js组件传值
    Vue.js安装
    C#中输入法全角转换半角
    文件夹操作
    转JSON字符串,并进行AES加密
    ReportView报表的使用
    c++读入优化
    快读板子
    【转】2020年 大二上 ACM
  • 原文地址:https://www.cnblogs.com/bestxyl/p/7383205.html
Copyright © 2020-2023  润新知