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

  

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

     /* *   *日期操作工具类。   * @Project ERPForAndroid   * @Package com.ymerp.android.tools   * @author chenlin   * @version 1.0   */@SuppressLint (“SimpleDateFormat”)   公开课DateUtil {   私有静态最终字符串格式=皔yyy-MM-dd HH: mm: ss”;   私有静态最终SimpleDateFormat datetimeFormat=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss”);   私有静态最终SimpleDateFormat dateFormat=new SimpleDateFormat (“yyyy-MM-dd”);   私有静态最终SimpleDateFormat timeFormat=new SimpleDateFormat (“HH: mm: ss”);   公共静态日期str2Date (String str) {   返回str2Date (str, null);   }/* *   *字符串转时间   * @param str   * @param格式   * @return   */公共静态日期str2Date (String str,字符串格式){   如果(str==null | | str.length ()==0) {   返回null;   }   如果(格式==null | | format.length ()==0) {   格式=格式;   }   日期日期=零;   尝试{   SimpleDateFormat自卫队=new SimpleDateFormat(格式);   日期=sdf.parse (str);   }捕捉(异常e) {   e.printStackTrace ();   }   返回日期;   }   公共静态日历str2Calendar (String str) {   返回str2Calendar (str, null);   }   公共静态日历str2Calendar (String str,字符串格式){   日期日期=str2Date (str,格式);   如果(日期==null) {   返回null;   }   日历c=Calendar.getInstance ();   c.setTime(日期);   返回c;   }   公共静态字符串date2Str(日历c) {//yyyy-MM-dd HH: mm: ss   返回date2Str (c, null);   }   公共静态字符串date2Str(日历c字符串格式){   如果(c==null) {   返回null;   }   返回date2Str (c.getTime(),格式);   }   公共静态字符串date2Str(日期d) {//yyyy-MM-dd HH: mm: ss   返回date2Str (d, null);   }   公共静态字符串date2Str (d,日期字符串格式){//yyyy-MM-dd HH: mm: ss   如果(d==null) {   返回null;   }   如果(格式==null | | format.length ()==0) {   格式=格式;   }   SimpleDateFormat自卫队=new SimpleDateFormat(格式);   字符串s=sdf.format (d);   返回年代;   }   公共静态字符串getCurDateStr () {   日历c=Calendar.getInstance ();   c。凝固时间(新日期());   返回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格式   * @return   */getCurDateStr公共静态字符串(字符串格式){   日历c=Calendar.getInstance ();   返回date2Str (c,格式);   }/* *   *格式到秒   *   * @param时间   * @return   */公共静态字符串getMillon(长时间){   返回新SimpleDateFormat (yyyy-MM-dd-HH-mm-ss) .format(时间);   }/* *   *格式到天   *   * @param时间   * @return   */公共静态字符串getDay(长时间){   返回新SimpleDateFormat (yyyy-MM-dd) .format(时间);   }/* *   *格式到毫秒   *   * @param时间   * @return   */公共静态字符串getSMillon(长时间){   返回新SimpleDateFormat (yyyy-MM-dd-HH-mm-ss-SSS) .format(时间);   }/* *   *字符串转换到时间格式   *   * @param dateStr   *需要转换的字符串   * @param formatStr   *需要格式的目标字符串举例yyyy-MM-dd   * @return日期返回转换后的时间   * @throws ParseException   *转换异常   */dateStr公共静态日期StringToDate(字符串,字符串formatStr) {   DateFormat自卫队=new SimpleDateFormat (formatStr);   日期日期=零;   尝试{   日期=sdf.parse (dateStr);   }捕捉(ParseException e) {   e.printStackTrace ();   }   返回日期;   }/* *   *转化时间输入时间与当前时间的间隔   *   * @param时间戳   * @return   */公共静态字符串converTime(长时间戳){   长currentSeconds=System.currentTimeMillis ()/1000;   长timeGap=currentSeconds -时间戳;//与现在时间相差秒数   字符串timeStr=零;   如果(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 +”分钟前”;   其他}{//1秒钟-59秒钟   timeStr="刚刚”;   }   返回timeStr;   }/* *   *把字符串转化为时间格式   *   * @param时间戳   * @return   */公共静态字符串getStandardTime(长时间戳){   SimpleDateFormat自卫队=new SimpleDateFormat (“MM月dd日HH: MM”);   日期日期=new日期(时间戳* 1000);   sdf.format(日期);   返回sdf.format(日期);   }/* *   *获得当前日期时间日期时间格式yyyy-MM-dd HH: mm: ss   *   * @return   */公共静态字符串currentDatetime () {   现在返回datetimeFormat.format (());   }/* *   *格式化日期时间日期时间格式yyyy-MM-dd HH: mm: ss   *   * @return   */公共静态字符串formatDatetime(日期日期){   返回datetimeFormat.format(日期);   }/* *   *获得当前时间时间格式HH: mm: ss   *   * @return   */公共静态字符串currentTime () {   现在返回timeFormat.format (());   }/* *   *格式化时间时间格式HH: mm: ss   *   * @return   */公共静态字符串formatTime(日期日期){   返回timeFormat.format(日期);   }/* *   *获得当前时间的& lt; code> java.util.Date

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