本文實(shí)例講述了Android開發(fā)中日期工具類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);  }  /**   * 字符串轉(zhuǎn)時(shí)間   * @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);  }  /**   * 獲得當(dāng)前日期的字符串格式   *   * @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);  }  /**   * 字符串轉(zhuǎn)換到時(shí)間格式   *   * @param dateStr   *      需要轉(zhuǎn)換的字符串   * @param formatStr   *      需要格式的目標(biāo)字符串 舉例 yyyy-MM-dd   * @return Date 返回轉(zhuǎn)換后的時(shí)間   * @throws ParseException   *       轉(zhuǎn)換異常   */  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;  }  /**   * 轉(zhuǎn)化時(shí)間輸入時(shí)間與當(dāng)前時(shí)間的間隔   *   * @param timestamp   * @return   */  public static String converTime(long timestamp) {    long currentSeconds = System.currentTimeMillis() / 1000;    long timeGap = currentSeconds - timestamp;// 與現(xiàn)在時(shí)間相差秒數(shù)    String timeStr = null;    if (timeGap > 24 * 60 * 60) {// 1天以上      timeStr = timeGap / (24 * 60 * 60) + "天前";    } else if (timeGap > 60 * 60) {// 1小時(shí)-24小時(shí)      timeStr = timeGap / (60 * 60) + "小時(shí)前";    } else if (timeGap > 60) {// 1分鐘-59分鐘      timeStr = timeGap / 60 + "分鐘前";    } else {// 1秒鐘-59秒鐘      timeStr = "剛剛";    }    return timeStr;  }  /**   * 把字符串轉(zhuǎn)化為時(shí)間格式   *   * @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);  }  /**   * 獲得當(dāng)前日期時(shí)間 日期時(shí)間格式y(tǒng)yyy-MM-dd HH:mm:ss   *   * @return   */  public static String currentDatetime() {    return datetimeFormat.format(now());  }  /**   * 格式化日期時(shí)間 日期時(shí)間格式y(tǒng)yyy-MM-dd HH:mm:ss   *   * @return   */  public static String formatDatetime(Date date) {    return datetimeFormat.format(date);  }  /**   * 獲得當(dāng)前時(shí)間 時(shí)間格式HH:mm:ss   *   * @return   */  public static String currentTime() {    return timeFormat.format(now());  }  /**   * 格式化時(shí)間 時(shí)間格式HH:mm:ss   *   * @return   */  public static String formatTime(Date date) {    return timeFormat.format(date);  }  /**   * 獲得當(dāng)前時(shí)間的<code>java.util.Date</code>對(duì)象   *   * @return   */  public static Date now() {    return new Date();  }  public static Calendar calendar() {    Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);    cal.setFirstDayOfWeek(Calendar.MONDAY);    return cal;  }  /**   * 獲得當(dāng)前時(shí)間的毫秒數(shù)   *   * 詳見{@link System#currentTimeMillis()}   *   * @return   */  public static long millis() {    return System.currentTimeMillis();  }  /**   *   * 獲得當(dāng)前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);  }  /**   * 判斷原日期是否在目標(biāo)日期之前   *   * @param src   * @param dst   * @return   */  public static boolean isBefore(Date src, Date dst) {    return src.before(dst);  }  /**   * 判斷原日期是否在目標(biāo)日期之后   *   * @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;  }  /**   * 判斷某個(gè)日期是否在某個(gè)日期范圍   *   * @param beginDate   *      日期范圍開始   * @param endDate   *      日期范圍結(jié)束   * @param src   *      需要判斷的日期   * @return   */  public static boolean between(Date beginDate, Date endDate, Date src) {    return beginDate.before(src) && endDate.after(src);  }  /**   * 獲得當(dāng)前月的最后一天   *   * 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();  }  /**   * 獲得當(dāng)前月的第一天   *   * 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()}設(shè)置類每個(gè)星期的第一天為Monday,US等每星期第一天為sunday   *   * @return   */  public static Date friday() {    return weekDay(Calendar.FRIDAY);  }  /**   * 獲得周六日期   *   * 注:日歷工廠方法{@link #calendar()}設(shè)置類每個(gè)星期的第一天為Monday,US等每星期第一天為sunday   *   * @return   */  public static Date saturday() {    return weekDay(Calendar.SATURDAY);  }  /**   * 獲得周日日期 注:日歷工廠方法{@link #calendar()}設(shè)置類每個(gè)星期的第一天為Monday,US等每星期第一天為sunday   *   * @return   */  public static Date sunday() {    return weekDay(Calendar.SUNDAY);  }  /**   * 將字符串日期時(shí)間轉(zhuǎn)換成java.util.Date類型 日期時(shí)間格式y(tǒng)yyy-MM-dd HH:mm:ss   *   * @param datetime   * @return   */  public static Date parseDatetime(String datetime) throws ParseException {    return datetimeFormat.parse(datetime);  }  /**   * 將字符串日期轉(zhuǎn)換成java.util.Date類型 日期時(shí)間格式y(tǒng)yyy-MM-dd   *   * @param date   * @return   * @throws ParseException   */  public static Date parseDate(String date) throws ParseException {    return dateFormat.parse(date);  }  /**   * 將字符串日期轉(zhuǎn)換成java.util.Date類型 時(shí)間格式 HH:mm:ss   *   * @param time   * @return   * @throws ParseException   */  public static Date parseTime(String time) throws ParseException {    return timeFormat.parse(time);  }  /**   * 根據(jù)自定義pattern將字符串日期轉(zhuǎn)換成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);  }  /**   * 把秒格式化為分種小時(shí)   *   * @param second   * @return   */  public static String parseSecond(int second) {    if (second >= 60) {      return second / 60 + "分";    } else if (second >= 60 * 60) {      return second / 60 * 60 + "時(shí)";    } else if (second >= 60 * 60 * 24) {      return second / 60 * 60 + "天";    } else {      return second + "秒";    }  }  /**   * 比較時(shí)間大小   * @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);  }  /**   * 獲得天數(shù)差   * @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;  }}希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注