android 获取英文日期格式化,android 字符串工具類 格式化日期字符串 拼接數組 判斷字符串是否為空 轉換時間顯示 轉換文件大小 獲取中英文字符個數...

android 字符串工具類 格式化日期字符串 拼接數組 判斷字符串是否為空 轉換時間顯示 轉換文件大小 獲取中英文字符個數

package com.nmbb.oplayer.util;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.Iterator;

import java.util.TimeZone;

import android.text.TextPaint;

import com.nmbb.oplayer.exception.Logger;

/**

* 字符串工具類

*/

public class StringUtils {

public static final String EMPTY = "";

private static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

private static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd hh:mm:ss";

/** 用於生成文件 */

private static final String DEFAULT_FILE_PATTERN = "yyyy-MM-dd-HH-mm-ss";

private static final double KB = 1024.0;

private static final double MB = 1048576.0;

private static final double GB = 1073741824.0;

public static final SimpleDateFormat DATE_FORMAT_PART = new SimpleDateFormat(

"HH:mm");

public static String currentTimeString() {

return DATE_FORMAT_PART.format(Calendar.getInstance().getTime());

}

public static char chatAt(String pinyin, int index) {

if (pinyin != null && pinyin.length() > 0)

return pinyin.charAt(index);

return ' ';

}

/** 獲取字符串寬度 */

public static float GetTextWidth(String Sentence, float Size) {

if (isEmpty(Sentence))

return 0;

TextPaint FontPaint = new TextPaint();

FontPaint.setTextSize(Size);

return FontPaint.measureText(Sentence.trim()) + (int) (Size * 0.1); // 留點余地

}

/**

* 格式化日期字符串

*

* @param date

* @param pattern

* @return

*/

public static String formatDate(Date date, String pattern) {

SimpleDateFormat format = new SimpleDateFormat(pattern);

return format.format(date);

}

/**

* 格式化日期字符串

*

* @param date

* @return 例如2011-3-24

*/

public static String formatDate(Date date) {

return formatDate(date, DEFAULT_DATE_PATTERN);

}

public static String formatDate(long date) {

return formatDate(new Date(date), DEFAULT_DATE_PATTERN);

}

/**

* 獲取當前時間 格式為yyyy-MM-dd 例如2011-07-08

*

* @return

*/

public static String getDate() {

return formatDate(new Date(), DEFAULT_DATE_PATTERN);

}

/** 生成一個文件名,不含后綴 */

public static String createFileName() {

Date date = new Date(System.currentTimeMillis());

SimpleDateFormat format = new SimpleDateFormat(DEFAULT_FILE_PATTERN);

return format.format(date);

}

/**

* 獲取當前時間

*

* @return

*/

public static String getDateTime() {

return formatDate(new Date(), DEFAULT_DATETIME_PATTERN);

}

/**

* 格式化日期時間字符串

*

* @param date

* @return 例如2011-11-30 16:06:54

*/

public static String formatDateTime(Date date) {

return formatDate(date, DEFAULT_DATETIME_PATTERN);

}

public static String formatDateTime(long date) {

return formatDate(new Date(date), DEFAULT_DATETIME_PATTERN);

}

/**

* 格林威時間轉換

*

* @param gmt

* @return

*/

public static String formatGMTDate(String gmt) {

TimeZone timeZoneLondon = TimeZone.getTimeZone(gmt);

return formatDate(Calendar.getInstance(timeZoneLondon)

.getTimeInMillis());

}

/**

* 拼接數組

*

* @param array

* @param separator

* @return

*/

public static String join(final ArrayList array,

final String separator) {

StringBuffer result = new StringBuffer();

if (array != null && array.size() > 0) {

for (String str : array) {

result.append(str);

result.append(separator);

}

result.delete(result.length() - 1, result.length());

}

return result.toString();

}

public static String join(final Iterator iter,

final String separator) {

StringBuffer result = new StringBuffer();

if (iter != null) {

while (iter.hasNext()) {

String key = iter.next();

result.append(key);

result.append(separator);

}

if (result.length() > 0)

result.delete(result.length() - 1, result.length());

}

return result.toString();

}

/**

* 判斷字符串是否為空

*

* @param str

* @return

*/

public static boolean isEmpty(String str) {

return str == null || str.length() == 0;

}

/**

*

* @param str

* @return

*/

public static String trim(String str) {

return str == null ? EMPTY : str.trim();

}

/**

* 轉換時間顯示

*

* @param time

* 毫秒

* @return

*/

public static String generateTime(long time) {

int totalSeconds = (int) (time / 1000);

int seconds = totalSeconds % 60;

int minutes = (totalSeconds / 60) % 60;

int hours = totalSeconds / 3600;

return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes,

seconds) : String.format("%02d:%02d", minutes, seconds);

}

/** 根據秒速獲取時間格式 */

public static String gennerTime(int totalSeconds) {

int seconds = totalSeconds % 60;

int minutes = (totalSeconds / 60) % 60;

return String.format("%02d:%02d", minutes, seconds);

}

/**

* 轉換文件大小

*

* @param size

* @return

*/

public static String generateFileSize(long size) {

String fileSize;

if (size < KB)

fileSize = size + "B";

else if (size < MB)

fileSize = String.format("%.1f", size / KB) + "KB";

else if (size < GB)

fileSize = String.format("%.1f", size / MB) + "MB";

else

fileSize = String.format("%.1f", size / GB) + "GB";

return fileSize;

}

public static String getTimeDiff(long time) {

// Calendar cal = Calendar.getInstance();

long diff = 0;

// Date dnow = cal.getTime();

String str = "";

diff = System.currentTimeMillis() - time;

if (diff > 2592000000L) {// 30 * 24 * 60 * 60 * 1000=2592000000 毫秒

str = "1個月前";

} else if (diff > 1814400000) {// 21 * 24 * 60 * 60 * 1000=1814400000 毫秒

str = "3周前";

} else if (diff > 1209600000) {// 14 * 24 * 60 * 60 * 1000=1209600000 毫秒

str = "2周前";

} else if (diff > 604800000) {// 7 * 24 * 60 * 60 * 1000=604800000 毫秒

str = "1周前";

} else if (diff > 86400000) { // 24 * 60 * 60 * 1000=86400000 毫秒

// System.out.println("X天前");

str = (int) Math.floor(diff / 86400000f) + "天前";

} else if (diff > 18000000) {// 5 * 60 * 60 * 1000=18000000 毫秒

// System.out.println("X小時前");

str = (int) Math.floor(diff / 18000000f) + "小時前";

} else if (diff > 60000) {// 1 * 60 * 1000=60000 毫秒

// System.out.println("X分鍾前");

str = (int) Math.floor(diff / 60000) + "分鍾前";

} else {

str = (int) Math.floor(diff / 1000) + "秒前";

}

return str;

}

/**

* 截取字符串

*

* @param search

* 待搜索的字符串

* @param start

* 起始字符串 例如:

* @param end

* 結束字符串 例如:

* @param defaultValue

* @return

*/

public static String substring(String search, String start, String end,

String defaultValue) {

int start_len = start.length();

int start_pos = StringUtils.isEmpty(start) ? 0 : search.indexOf(start);

if (start_pos > -1) {

int end_pos = StringUtils.isEmpty(end) ? -1 : search.indexOf(end,

start_pos + start_len);

if (end_pos > -1)

return search.substring(start_pos + start.length(), end_pos);

else

return search.substring(start_pos + start.length());

}

return defaultValue;

}

/**

* 截取字符串

*

* @param search

* 待搜索的字符串

* @param start

* 起始字符串 例如:

* @param end

* 結束字符串 例如:

* @return

*/

public static String substring(String search, String start, String end) {

return substring(search, start, end, "");

}

/**

* 拼接字符串

*

* @param strs

* @return

*/

public static String concat(String... strs) {

StringBuffer result = new StringBuffer();

if (strs != null) {

for (String str : strs) {

if (str != null)

result.append(str);

}

}

return result.toString();

}

/** 獲取中文字符個數 */

public static int getChineseCharCount(String str) {

String tempStr;

int count = 0;

for (int i = 0; i < str.length(); i++) {

tempStr = String.valueOf(str.charAt(i));

if (tempStr.getBytes().length == 3) {

count++;

}

}

return count;

}

/** 獲取英文字符個數 */

public static int getEnglishCount(String str) {

String tempStr;

int count = 0;

for (int i = 0; i < str.length(); i++) {

tempStr = String.valueOf(str.charAt(i));

if (!(tempStr.getBytes().length == 3)) {

count++;

}

}

return count;

}

public static String encode(String url) {

try {

return URLEncoder.encode(url, "UTF-8");

} catch (UnsupportedEncodingException e) {

Logger.e(e);

}

return url;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值