zoukankan      html  css  js  c++  java
  • android开发时间和日期的代码实现工具类(一)

    android开发时间和日期工具类的代码实现:

      1 package com.gzcivil.utils;
      2 
      3 import android.annotation.SuppressLint;
      4 import android.util.Log;
      5 
      6 import java.text.DateFormat;
      7 import java.text.ParseException;
      8 import java.text.ParsePosition;
      9 import java.text.SimpleDateFormat;
     10 import java.util.Calendar;
     11 import java.util.Date;
     12 
     13 /**
     14  * @see 时间、日期工具类
     15  * @author Chenxy
     16  * @date 2015-12-16 10:40
     17  */
     18 public class DateUtil {
     19 
     20     static SimpleDateFormat format;
     21 
     22     /** 日期格式:yyyy-MM-dd HH:mm:ss **/
     23     public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
     24 
     25     /** 日期格式:yyyy-MM-dd HH:mm **/
     26     public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
     27 
     28     /** 日期格式:yyyy-MM-dd **/
     29     public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
     30 
     31     /** 日期格式:HH:mm:ss **/
     32     public static final String DF_HH_MM_SS = "HH:mm:ss";
     33 
     34     /** 日期格式:HH:mm **/
     35     public static final String DF_HH_MM = "HH:mm";
     36 
     37     private final static long minute = 60 * 1000;// 1分钟
     38     private final static long hour = 60 * minute;// 1小时
     39     private final static long day = 24 * hour;// 1天
     40     private final static long month = 31 * day;//
     41     private final static long year = 12 * month;//
     42 
     43     /** Log输出标识 **/
     44     private static final String TAG = DateUtil.class.getSimpleName();
     45 
     46     public DateUtil() {
     47 
     48     }
     49 
     50     /**
     51      * 将日期格式化成友好的字符串:几分钟前、几小时前、几天前、几月前、几年前、刚刚
     52      * 
     53      * @param date
     54      * @return
     55      */
     56     public static String formatFriendly(Date date) {
     57         if (date == null) {
     58             return null;
     59         }
     60         long diff = new Date().getTime() - date.getTime();
     61         long r = 0;
     62         if (diff > year) {
     63             r = (diff / year);
     64             return r + "年前";
     65         }
     66         if (diff > month) {
     67             r = (diff / month);
     68             return r + "个月前";
     69         }
     70         if (diff > day) {
     71             r = (diff / day);
     72             return r + "天前";
     73         }
     74         if (diff > hour) {
     75             r = (diff / hour);
     76             return r + "个小时前";
     77         }
     78         if (diff > minute) {
     79             r = (diff / minute);
     80             return r + "分钟前";
     81         }
     82         return "刚刚";
     83     }
     84 
     85     /**
     86      * 将日期以yyyy-MM-dd HH:mm:ss格式化
     87      * 
     88      * @param dateL
     89      *            日期
     90      * @return
     91      */
     92     @SuppressLint("SimpleDateFormat")
     93     public static String formatDateTime(long dateL) {
     94         SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
     95         Date date = new Date(dateL);
     96         return sdf.format(date);
     97     }
     98 
     99     /**
    100      * 将日期以yyyy-MM-dd HH:mm:ss格式化
    101      * 
    102      * @param dateL
    103      *            日期
    104      * @return
    105      */
    106     @SuppressLint("SimpleDateFormat")
    107     public static String formatDateTime(long dateL, String formater) {
    108         SimpleDateFormat sdf = new SimpleDateFormat(formater);
    109         return sdf.format(new Date(dateL));
    110     }
    111 
    112     /**
    113      * 将日期以yyyy-MM-dd HH:mm:ss格式化
    114      * 
    115      * @param dateL
    116      *            日期
    117      * @return
    118      */
    119     @SuppressLint("SimpleDateFormat")
    120     public static String formatDateTime(Date date, String formater) {
    121         SimpleDateFormat sdf = new SimpleDateFormat(formater);
    122         return sdf.format(date);
    123     }
    124 
    125     /**
    126      * 将日期字符串转成日期
    127      * 
    128      * @param strDate
    129      *            字符串日期
    130      * @return java.util.date日期类型
    131      */
    132     @SuppressLint("SimpleDateFormat")
    133     public static Date parseDate(String strDate) {
    134         DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
    135         Date returnDate = null;
    136         try {
    137             returnDate = dateFormat.parse(strDate);
    138         } catch (ParseException e) {
    139             Log.v(TAG, "parseDate failed !");
    140 
    141         }
    142         return returnDate;
    143 
    144     }
    145 
    146     /**
    147      * 获取系统当前日期
    148      * 
    149      * @return
    150      */
    151     public static Date gainCurrentDate() {
    152         return new Date();
    153     }
    154 
    155     /**
    156      * 验证日期是否比当前日期早
    157      * 
    158      * @param target1
    159      *            比较时间1
    160      * @param target2
    161      *            比较时间2
    162      * @return true 则代表target1比target2晚或等于target2,否则比target2早
    163      */
    164     public static boolean compareDate(Date target1, Date target2) {
    165         boolean flag = false;
    166         try {
    167             String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
    168             String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
    169             if (target1DateTime.compareTo(target2DateTime) <= 0) {
    170                 flag = true;
    171             }
    172         } catch (Exception e) {
    173             System.out.println("比较失败,原因:" + e.getMessage());
    174         }
    175         return flag;
    176     }
    177 
    178     /**
    179      * 对日期进行增加操作
    180      * 
    181      * @param target
    182      *            需要进行运算的日期
    183      * @param hour
    184      *            小时
    185      * @return
    186      */
    187     public static Date addDateTime(Date target, double hour) {
    188         if (null == target || hour < 0) {
    189             return target;
    190         }
    191 
    192         return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
    193     }
    194 
    195     /**
    196      * 对日期进行相减操作
    197      * 
    198      * @param target
    199      *            需要进行运算的日期
    200      * @param hour
    201      *            小时
    202      * @return
    203      */
    204     public static Date subDateTime(Date target, double hour) {
    205         if (null == target || hour < 0) {
    206             return target;
    207         }
    208 
    209         return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
    210     }
    211 
    212     /** 获取系统时间的方法:月/日 时:分:秒 */
    213     public static String getFormateDate() {
    214         Calendar calendar = Calendar.getInstance();
    215         int month = (calendar.get(Calendar.MONTH) + 1);
    216         int day = calendar.get(Calendar.DAY_OF_MONTH);
    217         int hour = calendar.get(Calendar.HOUR_OF_DAY);
    218         int minute = calendar.get(Calendar.MINUTE);
    219         int second = calendar.get(Calendar.SECOND);
    220 
    221         String systemTime = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day) + "  " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
    222         return systemTime;
    223     }
    224 
    225     /** 获取系统时间的方法:时:分:秒 */
    226     public static String getHourAndMinute() {
    227         Calendar calendar = Calendar.getInstance();
    228         int hour = calendar.get(Calendar.HOUR_OF_DAY);
    229         int minute = calendar.get(Calendar.MINUTE);
    230         return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
    231     }
    232 
    233     /** 获取系统时间的方法:时 */
    234     public static String getHour() {
    235         Calendar calendar = Calendar.getInstance();
    236         int hour = calendar.get(Calendar.HOUR_OF_DAY);
    237         return ((hour < 10 ? "0" + hour : hour) + "");
    238     }
    239 
    240     /**
    241      * 将2014-09-10 00:00:00 换2014-09-10
    242      * 
    243      * @param strDate
    244      * @return
    245      */
    246     public static String strFormatStr(String strDate) {
    247         if (strDate.equals("")) {
    248             return "";
    249         }
    250         return dateToStr(strToDate(strDate));
    251     }
    252 
    253     /**
    254      * 2015-01-07 15:05:34
    255      * 
    256      * @param strDate
    257      * @return
    258      */
    259     @SuppressLint("SimpleDateFormat")
    260     public static Date strToDateHHMMSS(String strDate) {
    261         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    262         ParsePosition pos = new ParsePosition(0);
    263         Date strtodate = formatter.parse(strDate, pos);
    264         return strtodate;
    265     }
    266 
    267     /**
    268      * 2015-01-07
    269      * 
    270      * @param strDate
    271      * @return
    272      */
    273     @SuppressLint("SimpleDateFormat")
    274     public static Date strToDate(String strDate) {
    275         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    276         ParsePosition pos = new ParsePosition(0);
    277         Date strtodate = formatter.parse(strDate, pos);
    278         return strtodate;
    279     }
    280 
    281     /**
    282      * 2015.01.07
    283      * 
    284      * @param strDate
    285      * @return
    286      */
    287     @SuppressLint("SimpleDateFormat")
    288     public static Date strToDateDorp(String strDate) {
    289         SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
    290         ParsePosition pos = new ParsePosition(0);
    291         Date strtodate = formatter.parse(strDate, pos);
    292         return strtodate;
    293     }
    294 
    295     @SuppressLint("SimpleDateFormat")
    296     public static String dateToStr(java.util.Date dateDate) {
    297         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    298         String dateString = formatter.format(dateDate);
    299         return dateString;
    300     }
    301 
    302     /** 传入一个String转化为long */
    303     @SuppressLint("SimpleDateFormat")
    304     public static Long stringParserLong(String param) throws ParseException {
    305         format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    306         return format.parse(param).getTime();
    307     }
    308 
    309     /** 当前时间转换为long */
    310     @SuppressLint("SimpleDateFormat")
    311     public static Long currentDateParserLong() throws ParseException {
    312         format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    313         return format.parse(format.format(Calendar.getInstance().getTime())).getTime();
    314     }
    315 
    316     /** 当前时间 如: 2013-04-22 10:37:00 */
    317     @SuppressLint("SimpleDateFormat")
    318     public static String getCurrentDate() {
    319         format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    320         return format.format(Calendar.getInstance().getTime());
    321     }
    322 
    323     /** 当前时间 如: 10:37 */
    324     @SuppressLint("SimpleDateFormat")
    325     public static String getCurrentDateHHMM() {
    326         format = new SimpleDateFormat("HH:mm");
    327         return format.format(Calendar.getInstance().getTime());
    328     }
    329 
    330     /**
    331      * 当前时间 如: 10:37
    332      * 
    333      * @throws ParseException
    334      */
    335     @SuppressLint("SimpleDateFormat")
    336     public static String getCurrentDateHHMMSS() {
    337         format = new SimpleDateFormat("HH:mm:ss");
    338         return format.format(Calendar.getInstance().getTime());
    339     }
    340 
    341     /** 当前时间 如: 20130422 */
    342     @SuppressLint("SimpleDateFormat")
    343     public static String getCurrentDateString() {
    344         format = new SimpleDateFormat("yyyyMMddHHmm");
    345         return format.format(Calendar.getInstance().getTime());
    346     }
    347 
    348     /** 当前时间 如: 2013-04-22 */
    349     @SuppressLint("SimpleDateFormat")
    350     public static String getCurrentTime() {
    351         format = new SimpleDateFormat("yyyy-MM-dd");
    352         return format.format(Calendar.getInstance().getTime());
    353     }
    354 
    355     @SuppressLint("SimpleDateFormat")
    356     public static String getSWAHDate() {
    357         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    358         return format.format(Calendar.getInstance().getTime());
    359     }
    360 
    361     @SuppressLint("SimpleDateFormat")
    362     public static Long stringToLongD(String param) throws ParseException {
    363         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    364         return format.parse(param.substring(0, param.length() - 4)).getTime();
    365     }
    366 
    367     @SuppressLint("SimpleDateFormat")
    368     public static Long stringToLong(String param) throws ParseException {
    369         SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
    370         return format.parse(param).getTime();
    371     }
    372 
    373     /**
    374      * 获取两个日期之间的间隔天数
    375      * 
    376      * @return
    377      */
    378     @SuppressLint("SimpleDateFormat")
    379     public static int getGapCount(Date startDate, Date endDate) {
    380         Calendar fromCalendar = Calendar.getInstance();
    381         fromCalendar.setTime(startDate);
    382         fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
    383         fromCalendar.set(Calendar.MINUTE, 0);
    384         fromCalendar.set(Calendar.SECOND, 0);
    385         fromCalendar.set(Calendar.MILLISECOND, 0);
    386 
    387         Calendar toCalendar = Calendar.getInstance();
    388         toCalendar.setTime(endDate);
    389         toCalendar.set(Calendar.HOUR_OF_DAY, 0);
    390         toCalendar.set(Calendar.MINUTE, 0);
    391         toCalendar.set(Calendar.SECOND, 0);
    392         toCalendar.set(Calendar.MILLISECOND, 0);
    393 
    394         return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    395     }
    396 
    397     /**
    398      * 日期转换成Java字符串
    399      * 
    400      * @param date
    401      * @return str
    402      */
    403     @SuppressLint("SimpleDateFormat")
    404     public static String DateToStr(Date date) {
    405 
    406         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    407         String str = format.format(date);
    408         return str;
    409     }
    410 
    411     /**
    412      * 字符串转换成日期
    413      * 
    414      * @param str
    415      * @return date
    416      */
    417     @SuppressLint("SimpleDateFormat")
    418     public static Date StrToDate(String str) {
    419 
    420         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    421         Date date = null;
    422         try {
    423             date = format.parse(str);
    424         } catch (ParseException e) {
    425             e.printStackTrace();
    426         }
    427         return date;
    428     }
    429 
    430     /**
    431      * 字符串转换成日期
    432      * 
    433      * @param str
    434      * @return date
    435      */
    436     @SuppressLint("SimpleDateFormat")
    437     public static Date StrToDateDrop(String str) {
    438 
    439         SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
    440         Date date = null;
    441         try {
    442             date = format.parse(str);
    443         } catch (ParseException e) {
    444             e.printStackTrace();
    445         }
    446         return date;
    447     }
    448 
    449     /**
    450      * 
    451      * @param time
    452      * @return
    453      */
    454     @SuppressLint("SimpleDateFormat")
    455     public static long getLongTime(String time) {
    456         long ct = 0;
    457         try {
    458             format = new SimpleDateFormat("HH:mm:ss");
    459             ct = format.parse(time).getTime();
    460         } catch (Exception e) {
    461             e.printStackTrace();
    462         }
    463         return ct;
    464     }
    465 
    466     /**
    467      * 判断两日期是否同一天
    468      * 
    469      * @param str1
    470      * @param str2
    471      * @return
    472      */
    473     @SuppressLint("SimpleDateFormat")
    474     public static boolean isSameDay(String str1, String str2) {
    475 
    476         Date day1 = null, day2 = null;
    477         day1 = DateUtil.strToDate(str1);
    478         day2 = DateUtil.strToDate(str2);
    479 
    480         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    481 
    482         String ds1 = sdf.format(day1);
    483 
    484         String ds2 = sdf.format(day2);
    485 
    486         if (ds1.equals(ds2)) {
    487             return true;
    488         } else {
    489             return false;
    490         }
    491 
    492     }
    493 
    494     /**
    495      * 获取两个日期的时间差
    496      */
    497     @SuppressLint("SimpleDateFormat")
    498     public static int getTimeInterval(String date) {
    499         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    500         int interval = 0;
    501         try {
    502             Date currentTime = new Date();// 获取现在的时间
    503             Date beginTime = dateFormat.parse(date);
    504             interval = (int) ((beginTime.getTime() - currentTime.getTime()) / (1000));// 时间差
    505                                                                                         // 单位秒
    506         } catch (ParseException e) {
    507             e.printStackTrace();
    508         }
    509         return interval;
    510     }
    511 
    512     /**
    513      * 获取两个日期的时间差 yyyy.MM.dd HH.mm.ss
    514      */
    515     @SuppressLint("SimpleDateFormat")
    516     public static int getInterval(String bDate, String eDate) {
    517         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
    518         int interval = 0;
    519         try {
    520             Date currentTime = dateFormat.parse(eDate);// 获取现在的时间
    521             Date beginTime = dateFormat.parse(bDate);
    522             interval = (int) ((beginTime.getTime() - currentTime.getTime()));// 时间差
    523                                                                                 // 单位秒
    524         } catch (ParseException e) {
    525             e.printStackTrace();
    526         }
    527         return interval;
    528     }
    529 
    530     /**
    531      * 两个时间之差 求出一个long Time
    532      * 
    533      * @param dt1
    534      * @param dt2
    535      * @return
    536      */
    537     @SuppressLint("SimpleDateFormat")
    538     public static long getTime(String date) {
    539 
    540         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    541         long diff = 0;
    542         try {
    543             Date currentTime = new Date();// 获取现在的时间
    544             Date getdate = df.parse(date);
    545             diff = getdate.getTime() - currentTime.getTime();
    546 
    547         } catch (Exception e) {
    548         }
    549         return diff;
    550     }
    551 
    552     /**
    553      * 日期转换成Java字符串
    554      * 
    555      * @param date
    556      * @return str
    557      */
    558     @SuppressLint("SimpleDateFormat")
    559     public static int compare_date(String DATE1, String DATE2) {
    560         DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    561         try {
    562             Date dt1 = df.parse(DATE1);
    563             Date dt2 = df.parse(DATE2);
    564             if (dt1.getTime() >= dt2.getTime()) {
    565                 return 1;
    566             } else if (dt1.getTime() < dt2.getTime()) {
    567                 return -1;
    568             } else {
    569                 return 0;
    570             }
    571         } catch (Exception exception) {
    572             exception.printStackTrace();
    573         }
    574         return 0;
    575     }
    576 
    577     /**
    578      * 传入时间 算出星期几
    579      * 
    580      * @param str
    581      *            2014年1月3日
    582      * @param days
    583      *            1:2014年1月4日 类推
    584      * @return
    585      */
    586     @SuppressLint("SimpleDateFormat")
    587     public static String formatDate(String str, int days) {
    588 
    589         String dateStr = "";
    590         try {
    591             DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
    592             Date date = df.parse(str);
    593             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    594             Calendar c = Calendar.getInstance();
    595             Date d = dateFormat.parse(dateFormat.format(date));
    596             c.setTime(d);
    597             c.add(Calendar.DAY_OF_MONTH, days);
    598             switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
    599             case 0:
    600                 dateStr = "周日";
    601                 break;
    602             case 1:
    603                 dateStr = "周一";
    604                 break;
    605             case 2:
    606                 dateStr = "周二";
    607                 break;
    608             case 3:
    609                 dateStr = "周三";
    610                 break;
    611             case 4:
    612                 dateStr = "周四";
    613                 break;
    614             case 5:
    615                 dateStr = "周五";
    616                 break;
    617             case 6:
    618                 dateStr = "周六";
    619                 break;
    620             default:
    621                 break;
    622             }
    623         } catch (Exception e) {
    624             e.printStackTrace();
    625         }
    626 
    627         return dateStr;
    628     }
    629 }
  • 相关阅读:
    Linux菜鸟起飞之路【三】Linux常用命令
    Linux菜鸟起飞之路【二】Linux基本常识
    Linux菜鸟起飞之路【一】基本知识与Linux的安装
    交换机和路由器区别
    netdom join more ou
    keepalive.conf配置模板
    mysql7.7.22 Gtid主从搭建
    python 列表处理
    python openpyxl模块使用
    mysql5.7
  • 原文地址:https://www.cnblogs.com/lijinlun0825/p/5174673.html
Copyright © 2011-2022 走看看