欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发中日期工具类DateUtil完整实例

程序员文章站 2023-11-29 17:21:04
本文实例讲述了android开发中日期工具类dateutil。分享给大家供大家参考,具体如下: /** * 日期操作工具类. * @project er...

本文实例讲述了android开发中日期工具类dateutil。分享给大家供大家参考,具体如下:

/**
 * 日期操作工具类.
 * @project  erpforandroid
 * @package  com.ymerp.android.tools
 * @author   chenlin
 * @version  1.0
 */
@suppresslint("simpledateformat")
public class dateutil {
  private static final string format = "yyyy-mm-dd hh:mm:ss";
  private static final simpledateformat datetimeformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  private static final simpledateformat dateformat = new simpledateformat("yyyy-mm-dd");
  private static final simpledateformat timeformat = new simpledateformat("hh:mm:ss");
  public static date str2date(string str) {
    return str2date(str, null);
  }
  /**
   * 字符串转时间
   * @param str
   * @param format
   * @return
   */
  public static date str2date(string str, string format) {
    if (str == null || str.length() == 0) {
      return null;
    }
    if (format == null || format.length() == 0) {
      format = format;
    }
    date date = null;
    try {
      simpledateformat sdf = new simpledateformat(format);
      date = sdf.parse(str);
    } catch (exception e) {
      e.printstacktrace();
    }
    return date;
  }
  public static calendar str2calendar(string str) {
    return str2calendar(str, null);
  }
  public static calendar str2calendar(string str, string format) {
    date date = str2date(str, format);
    if (date == null) {
      return null;
    }
    calendar c = calendar.getinstance();
    c.settime(date);
    return c;
  }
  public static string date2str(calendar c) {// yyyy-mm-dd hh:mm:ss
    return date2str(c, null);
  }
  public static string date2str(calendar c, string format) {
    if (c == null) {
      return null;
    }
    return date2str(c.gettime(), format);
  }
  public static string date2str(date d) {// yyyy-mm-dd hh:mm:ss
    return date2str(d, null);
  }
  public static string date2str(date d, string format) {// yyyy-mm-dd hh:mm:ss
    if (d == null) {
      return null;
    }
    if (format == null || format.length() == 0) {
      format = format;
    }
    simpledateformat sdf = new simpledateformat(format);
    string s = sdf.format(d);
    return s;
  }
  public static string getcurdatestr() {
    calendar c = calendar.getinstance();
    c.settime(new date());
    return c.get(calendar.year) + "-" + (c.get(calendar.month) + 1) + "-" + c.get(calendar.day_of_month) + "-"
        + c.get(calendar.hour_of_day) + ":" + c.get(calendar.minute) + ":" + c.get(calendar.second);
  }
  /**
   * 获得当前日期的字符串格式
   *
   * @param format
   * @return
   */
  public static string getcurdatestr(string format) {
    calendar c = calendar.getinstance();
    return date2str(c, format);
  }
  /**
   * 格式到秒
   *
   * @param time
   * @return
   */
  public static string getmillon(long time) {
    return new simpledateformat("yyyy-mm-dd-hh-mm-ss").format(time);
  }
  /**
   * 格式到天
   *
   * @param time
   * @return
   */
  public static string getday(long time) {
    return new simpledateformat("yyyy-mm-dd").format(time);
  }
  /**
   * 格式到毫秒
   *
   * @param time
   * @return
   */
  public static string getsmillon(long time) {
    return new simpledateformat("yyyy-mm-dd-hh-mm-ss-sss").format(time);
  }
  /**
   * 字符串转换到时间格式
   *
   * @param datestr
   *      需要转换的字符串
   * @param formatstr
   *      需要格式的目标字符串 举例 yyyy-mm-dd
   * @return date 返回转换后的时间
   * @throws parseexception
   *       转换异常
   */
  public static date stringtodate(string datestr, string formatstr) {
    dateformat sdf = new simpledateformat(formatstr);
    date date = null;
    try {
      date = sdf.parse(datestr);
    } catch (parseexception e) {
      e.printstacktrace();
    }
    return date;
  }
  /**
   * 转化时间输入时间与当前时间的间隔
   *
   * @param timestamp
   * @return
   */
  public static string convertime(long timestamp) {
    long currentseconds = system.currenttimemillis() / 1000;
    long timegap = currentseconds - timestamp;// 与现在时间相差秒数
    string timestr = null;
    if (timegap > 24 * 60 * 60) {// 1天以上
      timestr = timegap / (24 * 60 * 60) + "天前";
    } else if (timegap > 60 * 60) {// 1小时-24小时
      timestr = timegap / (60 * 60) + "小时前";
    } else if (timegap > 60) {// 1分钟-59分钟
      timestr = timegap / 60 + "分钟前";
    } else {// 1秒钟-59秒钟
      timestr = "刚刚";
    }
    return timestr;
  }
  /**
   * 把字符串转化为时间格式
   *
   * @param timestamp
   * @return
   */
  public static string getstandardtime(long timestamp) {
    simpledateformat sdf = new simpledateformat("mm月dd日 hh:mm");
    date date = new date(timestamp * 1000);
    sdf.format(date);
    return sdf.format(date);
  }
  /**
   * 获得当前日期时间 日期时间格式yyyy-mm-dd hh:mm:ss
   *
   * @return
   */
  public static string currentdatetime() {
    return datetimeformat.format(now());
  }
  /**
   * 格式化日期时间 日期时间格式yyyy-mm-dd hh:mm:ss
   *
   * @return
   */
  public static string formatdatetime(date date) {
    return datetimeformat.format(date);
  }
  /**
   * 获得当前时间 时间格式hh:mm:ss
   *
   * @return
   */
  public static string currenttime() {
    return timeformat.format(now());
  }
  /**
   * 格式化时间 时间格式hh:mm:ss
   *
   * @return
   */
  public static string formattime(date date) {
    return timeformat.format(date);
  }
  /**
   * 获得当前时间的<code>java.util.date</code>对象
   *
   * @return
   */
  public static date now() {
    return new date();
  }
  public static calendar calendar() {
    calendar cal = gregoriancalendar.getinstance(locale.chinese);
    cal.setfirstdayofweek(calendar.monday);
    return cal;
  }
  /**
   * 获得当前时间的毫秒数
   *
   * 详见{@link system#currenttimemillis()}
   *
   * @return
   */
  public static long millis() {
    return system.currenttimemillis();
  }
  /**
   *
   * 获得当前chinese月份
   *
   * @return
   */
  public static int month() {
    return calendar().get(calendar.month) + 1;
  }
  /**
   * 获得月份中的第几天
   *
   * @return
   */
  public static int dayofmonth() {
    return calendar().get(calendar.day_of_month);
  }
  /**
   * 今天是星期的第几天
   *
   * @return
   */
  public static int dayofweek() {
    return calendar().get(calendar.day_of_week);
  }
  /**
   * 今天是年中的第几天
   *
   * @return
   */
  public static int dayofyear() {
    return calendar().get(calendar.day_of_year);
  }
  /**
   * 判断原日期是否在目标日期之前
   *
   * @param src
   * @param dst
   * @return
   */
  public static boolean isbefore(date src, date dst) {
    return src.before(dst);
  }
  /**
   * 判断原日期是否在目标日期之后
   *
   * @param src
   * @param dst
   * @return
   */
  public static boolean isafter(date src, date dst) {
    return src.after(dst);
  }
  /**
   * 判断两日期是否相同
   *
   * @param date1
   * @param date2
   * @return
   */
  public static boolean isequal(date date1, date date2) {
    return date1.compareto(date2) == 0;
  }
  /**
   * 判断某个日期是否在某个日期范围
   *
   * @param begindate
   *      日期范围开始
   * @param enddate
   *      日期范围结束
   * @param src
   *      需要判断的日期
   * @return
   */
  public static boolean between(date begindate, date enddate, date src) {
    return begindate.before(src) && enddate.after(src);
  }
  /**
   * 获得当前月的最后一天
   *
   * hh:mm:ss为0,毫秒为999
   *
   * @return
   */
  public static date lastdayofmonth() {
    calendar cal = calendar();
    cal.set(calendar.day_of_month, 0); // m月置零
    cal.set(calendar.hour_of_day, 0);// h置零
    cal.set(calendar.minute, 0);// m置零
    cal.set(calendar.second, 0);// s置零
    cal.set(calendar.millisecond, 0);// s置零
    cal.set(calendar.month, cal.get(calendar.month) + 1);// 月份+1
    cal.set(calendar.millisecond, -1);// 毫秒-1
    return cal.gettime();
  }
  /**
   * 获得当前月的第一天
   *
   * hh:mm:ss ss为零
   *
   * @return
   */
  public static date firstdayofmonth() {
    calendar cal = calendar();
    cal.set(calendar.day_of_month, 1); // m月置1
    cal.set(calendar.hour_of_day, 0);// h置零
    cal.set(calendar.minute, 0);// m置零
    cal.set(calendar.second, 0);// s置零
    cal.set(calendar.millisecond, 0);// s置零
    return cal.gettime();
  }
  private static date weekday(int week) {
    calendar cal = calendar();
    cal.set(calendar.day_of_week, week);
    return cal.gettime();
  }
  /**
   * 获得周五日期
   *
   * 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为monday,us等每星期第一天为sunday
   *
   * @return
   */
  public static date friday() {
    return weekday(calendar.friday);
  }
  /**
   * 获得周六日期
   *
   * 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为monday,us等每星期第一天为sunday
   *
   * @return
   */
  public static date saturday() {
    return weekday(calendar.saturday);
  }
  /**
   * 获得周日日期 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为monday,us等每星期第一天为sunday
   *
   * @return
   */
  public static date sunday() {
    return weekday(calendar.sunday);
  }
  /**
   * 将字符串日期时间转换成java.util.date类型 日期时间格式yyyy-mm-dd hh:mm:ss
   *
   * @param datetime
   * @return
   */
  public static date parsedatetime(string datetime) throws parseexception {
    return datetimeformat.parse(datetime);
  }
  /**
   * 将字符串日期转换成java.util.date类型 日期时间格式yyyy-mm-dd
   *
   * @param date
   * @return
   * @throws parseexception
   */
  public static date parsedate(string date) throws parseexception {
    return dateformat.parse(date);
  }
  /**
   * 将字符串日期转换成java.util.date类型 时间格式 hh:mm:ss
   *
   * @param time
   * @return
   * @throws parseexception
   */
  public static date parsetime(string time) throws parseexception {
    return timeformat.parse(time);
  }
  /**
   * 根据自定义pattern将字符串日期转换成java.util.date类型
   *
   * @param datetime
   * @param pattern
   * @return
   * @throws parseexception
   */
  public static date parsedatetime(string datetime, string pattern) throws parseexception {
    simpledateformat format = (simpledateformat) datetimeformat.clone();
    format.applypattern(pattern);
    return format.parse(datetime);
  }
  /**
   * 把秒格式化为分种小时
   *
   * @param second
   * @return
   */
  public static string parsesecond(int second) {
    if (second >= 60) {
      return second / 60 + "分";
    } else if (second >= 60 * 60) {
      return second / 60 * 60 + "时";
    } else if (second >= 60 * 60 * 24) {
      return second / 60 * 60 + "天";
    } else {
      return second + "秒";
    }
  }
  /**
   * 比较时间大小
   * @param begin
   * @param end
   * @return
   */
  public static int comparedate(string begin, string end) {
    dateformat df = new simpledateformat("yyyy-mm-dd hh:mm");
    try {
      date begindate = df.parse(begin);
      date enddate = df.parse(end);
      if (begindate.gettime() < enddate.gettime()) {
        return 1;
      } else if (begindate.gettime() > enddate.gettime()) {
        return -1;
      } else {
        return 0;
      }
    } catch (exception exception) {
      exception.printstacktrace();
    }
    return 0;
  }
  /**
   * 获得年份
   * @param date
   * @return
   */
  public int getyear(date date){
    calendar c = calendar.getinstance();
    c.settime(date);
    return c.get(calendar.year);
  }
  /**
   * 获得月份
   * @param date
   * @return
   */
  public int getmonth(date date){
    calendar c = calendar.getinstance();
    c.settime(date);
    return c.get(calendar.month) + 1;
  }
  /**
   * 获得星期几
   * @param date
   * @return
   */
  public int getweek(date date){
    calendar c = calendar.getinstance();
    c.settime(date);
    return c.get(calendar.week_of_year);
  }
  /**
   * 获得日期
   * @param date
   * @return
   */
  public int getday(date date){
    calendar c = calendar.getinstance();
    c.settime(date);
    return c.get(calendar.date);
  }
  /**
   * 获得天数差
   * @param begin
   * @param end
   * @return
   */
  public long getdaydiff(date begin, date end){
    long day = 1;
    if(end.gettime() < begin.gettime()){
      day = -1;
    }else if(end.gettime() == begin.gettime()){
      day = 1;
    }else {
      day += (end.gettime() - begin.gettime())/(24 * 60 * 60 * 1000) ;
    }
    return day;
  }
}

ps:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:

在线日期/天数计算器:

在线万年历日历:

在线阴历/阳历转换工具:

unix时间戳(timestamp)转换工具:

更多关于android相关内容感兴趣的读者可查看本站专题:《android日期与时间操作技巧总结》、《android开发入门与进阶教程》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。