Skip to content

Commit

Permalink
see 08/12 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Aug 12, 2016
1 parent 4aec11e commit b676d6e
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 145 deletions.
117 changes: 99 additions & 18 deletions utilcode/src/main/java/com/blankj/utilcode/utils/EncodeUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.blankj.utilcode.utils;

import android.annotation.TargetApi;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand All @@ -20,56 +26,131 @@ private EncodeUtils() {

/**
* URL编码
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String string, String charset)}方法</p>
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String input, String charset)}方法</p>
*
* @param string 要编码的字符
* @param input 要编码的字符
* @return 编码为UTF-8的字符串
*/
public static String urlEncode(String string) {
return urlEncode(string, "UTF-8");
public static String urlEncode(String input) {
return urlEncode(input, "UTF-8");
}

/**
* URL编码
* <p>若系统不支持指定的编码字符集,则直接将string原样返回</p>
* <p>若系统不支持指定的编码字符集,则直接将input原样返回</p>
*
* @param string 要编码的字符
* @param input 要编码的字符
* @param charset 字符集
* @return 编码为字符集的字符串
*/
public static String urlEncode(String string, String charset) {
public static String urlEncode(String input, String charset) {
try {
return URLEncoder.encode(string, charset);
return URLEncoder.encode(input, charset);
} catch (UnsupportedEncodingException e) {
return string;
return input;
}
}

/**
* URL解码
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String string, String charset)}方法</p>
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String input, String charset)}方法</p>
*
* @param string 要解码的字符串
* @param input 要解码的字符串
* @return URL解码后的字符串
*/
public static String urlDecode(String string) {
return urlDecode(string, "UTF-8");
public static String urlDecode(String input) {
return urlDecode(input, "UTF-8");
}

/**
* URL解码
* <p>若系统不支持指定的解码字符集,则直接将string原样返回</p>
* <p>若系统不支持指定的解码字符集,则直接将input原样返回</p>
*
* @param string 要解码的字符串
* @param input 要解码的字符串
* @param charset 字符集
* @return URL解码为指定字符集的字符串
*/
public static String urlDecode(String string, String charset) {
public static String urlDecode(String input, String charset) {
try {
return URLDecoder.decode(string, charset);
return URLDecoder.decode(input, charset);
} catch (UnsupportedEncodingException e) {
return string;
return input;
}
}

/**
* Base64编码.
*/
public static String base64Encode(String input) {
return Base64.encodeToString(input.getBytes(), Base64.DEFAULT);
}

/**
* Base64解码.
*/
public static String base64Decode(String input) {
return new String(Base64.decode(input, Base64.DEFAULT));
}

/**
* Base64URL安全编码
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
*/
public static String base64UrlSafeEncode(String input) {
return Base64.encodeToString(input.getBytes(), Base64.URL_SAFE);
}

/**
* Html编码
*
* @param input
* @return Html编码后的字符串
*/
public static String htmlEncode(String input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return Html.escapeHtml(input);
} else {
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("&lt;");
} else if (c == '>') {
out.append("&gt;");
} else if (c == '&') {
out.append("&amp;");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append("&nbsp;");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}

/**
* Html解码
*
* @param input 待解码的字符串
* @return Html解码后的字符串
*/
public static String htmlDecode(String input) {
return Html.fromHtml(input).toString();
}
}
48 changes: 48 additions & 0 deletions utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.blankj.utilcode.utils;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/12
* desc :
* </pre>
*/
public class ImageUtils {

/**
* 将Bitmap转换成字符串
*
* @param bitmap
* @return
*/
public static String bitmaptoString(Bitmap bitmap) {
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}

/**
* 把byte数组转化成 bitmap对象
*
* @param b
* @return
*/
public static Bitmap bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static float applyDimension(int unit, float value, DisplayMetrics metrics
* <pre>
* SizeUtils.forceGetViewSize(view);
* SizeUtils.setListener(new SizeUtils.onGetSizeListener() {
* <br>@Override
* </span>@Override
* public void onGetSize(View view) {
* Log.d("tag", view.getWidth() + " " + view.getHeight());
* }
Expand Down
68 changes: 41 additions & 27 deletions utilcode/src/main/java/com/blankj/utilcode/utils/TimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import static com.blankj.utilcode.utils.UnitUtils.*;

Expand Down Expand Up @@ -151,19 +152,31 @@ private TimeUtils() {
* </tr>
* </table>
* <pre>
* yyyy-MM-dd 1969-12-31
* yyyy-MM-dd 1970-01-01
* yyyy-MM-dd HH:mm 1969-12-31 16:00
* yyyy-MM-dd HH:mm 1970-01-01 00:00
* yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
* yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
* yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
* yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
* HH:mm 15:44
* h:mm a 3:44 下午
* HH:mm z 15:44 CST
* HH:mm Z 15:44 +0800
* HH:mm zzzz 15:44 中国标准时间
* HH:mm:ss 15:44:40
* yyyy-MM-dd 2016-08-12
* yyyy-MM-dd HH:mm 2016-08-12 15:44
* yyyy-MM-dd HH:mm:ss 2016-08-12 15:44:40
* yyyy-MM-dd HH:mm:ss zzzz 2016-08-12 15:44:40 中国标准时间
* EEEE yyyy-MM-dd HH:mm:ss zzzz 星期五 2016-08-12 15:44:40 中国标准时间
* yyyy-MM-dd HH:mm:ss.SSSZ 2016-08-12 15:44:40.461+0800
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
* yyyy.MM.dd G 'at' HH:mm:ss z 2016.08.12 公元 at 15:44:40 CST
* K:mm a 3:44 下午
* EEE, MMM d, ''yy 星期五, 八月 12, '16
* hh 'o''clock' a, zzzz 03 o'clock 下午, 中国标准时间
* yyyyy.MMMMM.dd GGG hh:mm aaa 02016.八月.12 公元 03:44 下午
* EEE, d MMM yyyy HH:mm:ss Z 星期五, 12 八月 2016 15:44:40 +0800
* yyMMddHHmmssZ 160812154440+0800
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
* EEEE 'DATE('yyyy-MM-dd')' 'TIME('HH:mm:ss')' zzzz 星期五 DATE(2016-08-12) TIME(15:44:40) 中国标准时间
* </pre>
*/
public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());


/**
Expand Down Expand Up @@ -303,7 +316,7 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
case MIN:
case HOUR:
case DAY:
return Math.abs(milliseconds) / unit;
return milliseconds / unit;
}
return -1;
}
Expand All @@ -312,8 +325,8 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
* 获取两个时间差(单位:unit)
* <p>time1和time2格式都为yyyy-MM-dd HH:mm:ss</p>
*
* @param time1 时间字符串1
* @param time2 时间字符串2
* @param time0 时间字符串1
* @param time1 时间字符串2
* @param unit <ul>
* <li>MSEC:毫秒</li>
* <li>SEC :秒</li>
Expand All @@ -323,16 +336,16 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
* </ul>
* @return unit时间戳
*/
public static long getIntervalTime(String time1, String time2, int unit) {
return getIntervalTime(time1, time2, unit, DEFAULT_SDF);
public static long getIntervalTime(String time0, String time1, int unit) {
return getIntervalTime(time0, time1, unit, DEFAULT_SDF);
}

/**
* 获取两个时间差(单位:unit)
* <p>time1和time2格式都为format</p>
*
* @param time1 时间字符串1
* @param time2 时间字符串2
* @param time0 时间字符串1
* @param time1 时间字符串2
* @param unit <ul>
* <li>MSEC:毫秒</li>
* <li>SEC :秒</li>
Expand All @@ -343,17 +356,17 @@ public static long getIntervalTime(String time1, String time2, int unit) {
* @param format 时间格式
* @return unit时间戳
*/
public static long getIntervalTime(String time1, String time2, int unit, SimpleDateFormat format) {
return milliseconds2Unit(string2Milliseconds(time1, format)
- string2Milliseconds(time2, format), unit);
public static long getIntervalTime(String time0, String time1, int unit, SimpleDateFormat format) {
return Math.abs(milliseconds2Unit(string2Milliseconds(time0, format)
- string2Milliseconds(time1, format), unit));
}

/**
* 获取两个时间差(单位:unit)
* <p>time1和time2都为Date类型</p>
*
* @param time1 Date类型时间1
* @param time2 Date类型时间2
* @param time0 Date类型时间1
* @param time1 Date类型时间2
* @param unit <ul>
* <li>MSEC:毫秒</li>
* <li>SEC :秒</li>
Expand All @@ -363,8 +376,9 @@ public static long getIntervalTime(String time1, String time2, int unit, SimpleD
* </ul>
* @return unit时间戳
*/
public static long getIntervalTime(Date time1, Date time2, int unit) {
return milliseconds2Unit(date2Milliseconds(time2) - date2Milliseconds(time1), unit);
public static long getIntervalTime(Date time0, Date time1, int unit) {
return Math.abs(milliseconds2Unit(date2Milliseconds(time1)
- date2Milliseconds(time0), unit));
}

/**
Expand All @@ -383,7 +397,7 @@ public static long getCurTimeMills() {
* @return 时间字符串
*/
public static String getCurTimeString() {
return milliseconds2String(getCurTimeMills());
return date2String(new Date());
}

/**
Expand All @@ -394,7 +408,7 @@ public static String getCurTimeString() {
* @return 时间字符串
*/
public static String getCurTimeString(SimpleDateFormat format) {
return milliseconds2String(getCurTimeMills(), format);
return date2String(new Date(), format);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.util.Base64;

import java.util.List;

Expand Down
Loading

0 comments on commit b676d6e

Please sign in to comment.