Android Date,時間字符串、時間戳等相互轉(zhuǎn)換使用

“yyyy-MM-dd HH:mm:ss” 這是常用的時間顯示格式,表示了“年-月-日 時:分:秒”

注意:HH表示24小時制,hh表示12小時制

1、日期字符串轉(zhuǎn)換Date

//參數(shù)一:時間字符串; 參數(shù)二:日期格式
public static Date parseServerTime(String serverTime, String format) {
    if (format == null || format.isEmpty()) {
        format = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
    sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    Date date = null;
    try {
        date = sdf.parse(serverTime);
    } catch (Exception e) {
        Timber.e(e, "");
    }
    return date;
}

2、秒數(shù)轉(zhuǎn)換成時分秒

public static String convertSecToTimeString(long lSeconds) {
    long nHour = lSeconds / 3600;
    long nMin = lSeconds % 3600;
    long nSec = nMin % 60;
    nMin = nMin / 60;

    return String.format("%02d小時%02d分鐘%02d秒", nHour, nMin, nSec);
}

3、Date對象獲取時間字符串

public static String getDateStr(Date date,String format) {
    if (format == null || format.isEmpty()) {
        format = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    return formatter.format(date);
}

4、時間戳轉(zhuǎn)換日期格式字符串

public static String timeStamp2Date(long time, String format) {
    if (format == null || format.isEmpty()) {
        format = "yyyy-MM-dd HH:mm:ss";
    }
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(new Date(time));
}

5、日期格式字符串轉(zhuǎn)換時間戳

public static String date2TimeStamp(String date, String format) {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return String.valueOf(sdf.parse(date).getTime() / 1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

6、獲取某個日期前后N天的日期

/**
 * 獲取某個日期前后N天的日期
 *
 * @param beginDate
 * @param distanceDay 前后幾天 如獲取前7天日期則傳-7即可;如果后7天則傳7
 * @param format      日期格式,默認(rèn)"yyyy-MM-dd"
 * @return
 */
public static String getOldDateByDay(Date beginDate, int distanceDay, String format) {
    if (format == null || format.isEmpty()) {
        format = "yyyy-MM-dd";
    }
    SimpleDateFormat dft = new SimpleDateFormat(format);
    Calendar date = Calendar.getInstance();
    date.setTime(beginDate);
    date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
    Date endDate = null;
    try {
        endDate = dft.parse(dft.format(date.getTime()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dft.format(endDate);
}

7、獲取前后幾個月的日期

/**
 * 獲取前后幾個月的日期
 * @param beginDate
 * @param distanceMonth
 * @param format
 * @return
 */
public static String getOldDateByMonth(Date beginDate, int distanceMonth, String format) {
    if (format == null || format.isEmpty()) {
        format = "yyyy-MM-dd";
    }
    SimpleDateFormat dft = new SimpleDateFormat(format);
    Calendar date = Calendar.getInstance();
    date.setTime(beginDate);
    date.set(Calendar.MONTH, date.get(Calendar.MONTH) + distanceMonth);
    Date endDate = null;
    try {
        endDate = dft.parse(dft.format(date.getTime()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dft.format(endDate);
}

8、判斷一個時間是否在某個時間范圍內(nèi)

 public static boolean isCurrentInTimeScope(int deadlineHour, int deadlineMin) {
     boolean result;
     // 1000 * 60 * 60 * 24
     final long aDayInMillis = 86400000;
     final long currentTimeMillis = SntpClock.currentTimeMillis();
     //截止時間
     Time deadlineTime = new Time();
     deadlineTime.set(currentTimeMillis);
     deadlineTime.hour = deadlineHour;
     deadlineTime.minute = deadlineMin;
     //當(dāng)前時間
     Time startTime = new Time();
     startTime.set(currentTimeMillis);
     //當(dāng)前時間推后20分鐘
     Date d = new Date(currentTimeMillis);
     long myTime = (d.getTime() / 1000) + 20 * 60;
     d.setTime(myTime * 1000);
     Time endTime = new Time();
     endTime.set(myTime);
     if (!startTime.before(endTime)) {
         // 跨天的特殊情況(比如22:00-8:00)
         startTime.set(startTime.toMillis(true) - aDayInMillis);
         result = !deadlineTime.before(startTime) && !deadlineTime.after(endTime); 
         // startTime <= deadlineTime <=endTime
         Time startTimeInThisDay = new Time();
         startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis);
         if (!deadlineTime.before(startTimeInThisDay)) {
             result = true;
         }
     } else {
         // 普通情況(比如 8:00 - 14:00)
         result = !deadlineTime.before(startTime) && !deadlineTime.after(endTime);
         // startTime <= deadlineTime <=endTime
        }
        return result;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容