Skip to content

Commit b676d6e

Browse files
committed
see 08/12 log
1 parent 4aec11e commit b676d6e

File tree

10 files changed

+370
-145
lines changed

10 files changed

+370
-145
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.blankj.utilcode.utils;
22

3+
import android.annotation.TargetApi;
4+
import android.os.Build;
5+
import android.text.Html;
6+
import android.text.TextUtils;
7+
import android.util.Base64;
8+
39
import java.io.UnsupportedEncodingException;
410
import java.net.URLDecoder;
511
import java.net.URLEncoder;
@@ -20,56 +26,131 @@ private EncodeUtils() {
2026

2127
/**
2228
* URL编码
23-
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String string, String charset)}方法</p>
29+
* <p>若想自己指定字符集,可以使用{@link #urlEncode(String input, String charset)}方法</p>
2430
*
25-
* @param string 要编码的字符
31+
* @param input 要编码的字符
2632
* @return 编码为UTF-8的字符串
2733
*/
28-
public static String urlEncode(String string) {
29-
return urlEncode(string, "UTF-8");
34+
public static String urlEncode(String input) {
35+
return urlEncode(input, "UTF-8");
3036
}
3137

3238
/**
3339
* URL编码
34-
* <p>若系统不支持指定的编码字符集,则直接将string原样返回</p>
40+
* <p>若系统不支持指定的编码字符集,则直接将input原样返回</p>
3541
*
36-
* @param string 要编码的字符
42+
* @param input 要编码的字符
3743
* @param charset 字符集
3844
* @return 编码为字符集的字符串
3945
*/
40-
public static String urlEncode(String string, String charset) {
46+
public static String urlEncode(String input, String charset) {
4147
try {
42-
return URLEncoder.encode(string, charset);
48+
return URLEncoder.encode(input, charset);
4349
} catch (UnsupportedEncodingException e) {
44-
return string;
50+
return input;
4551
}
4652
}
4753

4854
/**
4955
* URL解码
50-
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String string, String charset)}方法</p>
56+
* <p>若想自己指定字符集,可以使用 {@link #urlDecode(String input, String charset)}方法</p>
5157
*
52-
* @param string 要解码的字符串
58+
* @param input 要解码的字符串
5359
* @return URL解码后的字符串
5460
*/
55-
public static String urlDecode(String string) {
56-
return urlDecode(string, "UTF-8");
61+
public static String urlDecode(String input) {
62+
return urlDecode(input, "UTF-8");
5763
}
5864

5965
/**
6066
* URL解码
61-
* <p>若系统不支持指定的解码字符集,则直接将string原样返回</p>
67+
* <p>若系统不支持指定的解码字符集,则直接将input原样返回</p>
6268
*
63-
* @param string 要解码的字符串
69+
* @param input 要解码的字符串
6470
* @param charset 字符集
6571
* @return URL解码为指定字符集的字符串
6672
*/
67-
public static String urlDecode(String string, String charset) {
73+
public static String urlDecode(String input, String charset) {
6874
try {
69-
return URLDecoder.decode(string, charset);
75+
return URLDecoder.decode(input, charset);
7076
} catch (UnsupportedEncodingException e) {
71-
return string;
77+
return input;
7278
}
7379
}
7480

81+
/**
82+
* Base64编码.
83+
*/
84+
public static String base64Encode(String input) {
85+
return Base64.encodeToString(input.getBytes(), Base64.DEFAULT);
86+
}
87+
88+
/**
89+
* Base64解码.
90+
*/
91+
public static String base64Decode(String input) {
92+
return new String(Base64.decode(input, Base64.DEFAULT));
93+
}
94+
95+
/**
96+
* Base64URL安全编码
97+
* <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
98+
*/
99+
public static String base64UrlSafeEncode(String input) {
100+
return Base64.encodeToString(input.getBytes(), Base64.URL_SAFE);
101+
}
102+
103+
/**
104+
* Html编码
105+
*
106+
* @param input
107+
* @return Html编码后的字符串
108+
*/
109+
public static String htmlEncode(String input) {
110+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
111+
return Html.escapeHtml(input);
112+
} else {
113+
StringBuilder out = new StringBuilder();
114+
for (int i = 0, len = input.length(); i < len; i++) {
115+
char c = input.charAt(i);
116+
if (c == '<') {
117+
out.append("&lt;");
118+
} else if (c == '>') {
119+
out.append("&gt;");
120+
} else if (c == '&') {
121+
out.append("&amp;");
122+
} else if (c >= 0xD800 && c <= 0xDFFF) {
123+
if (c < 0xDC00 && i + 1 < len) {
124+
char d = input.charAt(i + 1);
125+
if (d >= 0xDC00 && d <= 0xDFFF) {
126+
i++;
127+
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
128+
out.append("&#").append(codepoint).append(";");
129+
}
130+
}
131+
} else if (c > 0x7E || c < ' ') {
132+
out.append("&#").append((int) c).append(";");
133+
} else if (c == ' ') {
134+
while (i + 1 < len && input.charAt(i + 1) == ' ') {
135+
out.append("&nbsp;");
136+
i++;
137+
}
138+
out.append(' ');
139+
} else {
140+
out.append(c);
141+
}
142+
}
143+
return out.toString();
144+
}
145+
}
146+
147+
/**
148+
* Html解码
149+
*
150+
* @param input 待解码的字符串
151+
* @return Html解码后的字符串
152+
*/
153+
public static String htmlDecode(String input) {
154+
return Html.fromHtml(input).toString();
155+
}
75156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.blankj.utilcode.utils;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.Bitmap.CompressFormat;
5+
import android.graphics.BitmapFactory;
6+
import android.util.Base64;
7+
8+
import java.io.ByteArrayOutputStream;
9+
10+
/**
11+
* <pre>
12+
* author: Blankj
13+
* blog : http://blankj.com
14+
* time : 2016/8/12
15+
* desc :
16+
* </pre>
17+
*/
18+
public class ImageUtils {
19+
20+
/**
21+
* 将Bitmap转换成字符串
22+
*
23+
* @param bitmap
24+
* @return
25+
*/
26+
public static String bitmaptoString(Bitmap bitmap) {
27+
String string = null;
28+
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
29+
bitmap.compress(CompressFormat.PNG, 100, bStream);
30+
byte[] bytes = bStream.toByteArray();
31+
string = Base64.encodeToString(bytes, Base64.DEFAULT);
32+
return string;
33+
}
34+
35+
/**
36+
* 把byte数组转化成 bitmap对象
37+
*
38+
* @param b
39+
* @return
40+
*/
41+
public static Bitmap bytes2Bimap(byte[] b) {
42+
if (b.length != 0) {
43+
return BitmapFactory.decodeByteArray(b, 0, b.length);
44+
} else {
45+
return null;
46+
}
47+
}
48+
}

utilcode/src/main/java/com/blankj/utilcode/utils/SizeUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static float applyDimension(int unit, float value, DisplayMetrics metrics
102102
* <pre>
103103
* SizeUtils.forceGetViewSize(view);
104104
* SizeUtils.setListener(new SizeUtils.onGetSizeListener() {
105-
* <br>@Override
105+
* </span>@Override
106106
* public void onGetSize(View view) {
107107
* Log.d("tag", view.getWidth() + " " + view.getHeight());
108108
* }

utilcode/src/main/java/com/blankj/utilcode/utils/TimeUtils.java

+41-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.text.ParseException;
44
import java.text.SimpleDateFormat;
55
import java.util.Date;
6+
import java.util.Locale;
67

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

@@ -151,19 +152,31 @@ private TimeUtils() {
151152
* </tr>
152153
* </table>
153154
* <pre>
154-
* yyyy-MM-dd 1969-12-31
155-
* yyyy-MM-dd 1970-01-01
156-
* yyyy-MM-dd HH:mm 1969-12-31 16:00
157-
* yyyy-MM-dd HH:mm 1970-01-01 00:00
158-
* yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
159-
* yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
160-
* yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
161-
* yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
162-
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
163-
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000
155+
* HH:mm 15:44
156+
* h:mm a 3:44 下午
157+
* HH:mm z 15:44 CST
158+
* HH:mm Z 15:44 +0800
159+
* HH:mm zzzz 15:44 中国标准时间
160+
* HH:mm:ss 15:44:40
161+
* yyyy-MM-dd 2016-08-12
162+
* yyyy-MM-dd HH:mm 2016-08-12 15:44
163+
* yyyy-MM-dd HH:mm:ss 2016-08-12 15:44:40
164+
* yyyy-MM-dd HH:mm:ss zzzz 2016-08-12 15:44:40 中国标准时间
165+
* EEEE yyyy-MM-dd HH:mm:ss zzzz 星期五 2016-08-12 15:44:40 中国标准时间
166+
* yyyy-MM-dd HH:mm:ss.SSSZ 2016-08-12 15:44:40.461+0800
167+
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
168+
* yyyy.MM.dd G 'at' HH:mm:ss z 2016.08.12 公元 at 15:44:40 CST
169+
* K:mm a 3:44 下午
170+
* EEE, MMM d, ''yy 星期五, 八月 12, '16
171+
* hh 'o''clock' a, zzzz 03 o'clock 下午, 中国标准时间
172+
* yyyyy.MMMMM.dd GGG hh:mm aaa 02016.八月.12 公元 03:44 下午
173+
* EEE, d MMM yyyy HH:mm:ss Z 星期五, 12 八月 2016 15:44:40 +0800
174+
* yyMMddHHmmssZ 160812154440+0800
175+
* yyyy-MM-dd'T'HH:mm:ss.SSSZ 2016-08-12T15:44:40.461+0800
176+
* EEEE 'DATE('yyyy-MM-dd')' 'TIME('HH:mm:ss')' zzzz 星期五 DATE(2016-08-12) TIME(15:44:40) 中国标准时间
164177
* </pre>
165178
*/
166-
public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
179+
public static final SimpleDateFormat DEFAULT_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
167180

168181

169182
/**
@@ -303,7 +316,7 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
303316
case MIN:
304317
case HOUR:
305318
case DAY:
306-
return Math.abs(milliseconds) / unit;
319+
return milliseconds / unit;
307320
}
308321
return -1;
309322
}
@@ -312,8 +325,8 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
312325
* 获取两个时间差(单位:unit)
313326
* <p>time1和time2格式都为yyyy-MM-dd HH:mm:ss</p>
314327
*
315-
* @param time1 时间字符串1
316-
* @param time2 时间字符串2
328+
* @param time0 时间字符串1
329+
* @param time1 时间字符串2
317330
* @param unit <ul>
318331
* <li>MSEC:毫秒</li>
319332
* <li>SEC :秒</li>
@@ -323,16 +336,16 @@ private static long milliseconds2Unit(long milliseconds, int unit) {
323336
* </ul>
324337
* @return unit时间戳
325338
*/
326-
public static long getIntervalTime(String time1, String time2, int unit) {
327-
return getIntervalTime(time1, time2, unit, DEFAULT_SDF);
339+
public static long getIntervalTime(String time0, String time1, int unit) {
340+
return getIntervalTime(time0, time1, unit, DEFAULT_SDF);
328341
}
329342

330343
/**
331344
* 获取两个时间差(单位:unit)
332345
* <p>time1和time2格式都为format</p>
333346
*
334-
* @param time1 时间字符串1
335-
* @param time2 时间字符串2
347+
* @param time0 时间字符串1
348+
* @param time1 时间字符串2
336349
* @param unit <ul>
337350
* <li>MSEC:毫秒</li>
338351
* <li>SEC :秒</li>
@@ -343,17 +356,17 @@ public static long getIntervalTime(String time1, String time2, int unit) {
343356
* @param format 时间格式
344357
* @return unit时间戳
345358
*/
346-
public static long getIntervalTime(String time1, String time2, int unit, SimpleDateFormat format) {
347-
return milliseconds2Unit(string2Milliseconds(time1, format)
348-
- string2Milliseconds(time2, format), unit);
359+
public static long getIntervalTime(String time0, String time1, int unit, SimpleDateFormat format) {
360+
return Math.abs(milliseconds2Unit(string2Milliseconds(time0, format)
361+
- string2Milliseconds(time1, format), unit));
349362
}
350363

351364
/**
352365
* 获取两个时间差(单位:unit)
353366
* <p>time1和time2都为Date类型</p>
354367
*
355-
* @param time1 Date类型时间1
356-
* @param time2 Date类型时间2
368+
* @param time0 Date类型时间1
369+
* @param time1 Date类型时间2
357370
* @param unit <ul>
358371
* <li>MSEC:毫秒</li>
359372
* <li>SEC :秒</li>
@@ -363,8 +376,9 @@ public static long getIntervalTime(String time1, String time2, int unit, SimpleD
363376
* </ul>
364377
* @return unit时间戳
365378
*/
366-
public static long getIntervalTime(Date time1, Date time2, int unit) {
367-
return milliseconds2Unit(date2Milliseconds(time2) - date2Milliseconds(time1), unit);
379+
public static long getIntervalTime(Date time0, Date time1, int unit) {
380+
return Math.abs(milliseconds2Unit(date2Milliseconds(time1)
381+
- date2Milliseconds(time0), unit));
368382
}
369383

370384
/**
@@ -383,7 +397,7 @@ public static long getCurTimeMills() {
383397
* @return 时间字符串
384398
*/
385399
public static String getCurTimeString() {
386-
return milliseconds2String(getCurTimeMills());
400+
return date2String(new Date());
387401
}
388402

389403
/**
@@ -394,7 +408,7 @@ public static String getCurTimeString() {
394408
* @return 时间字符串
395409
*/
396410
public static String getCurTimeString(SimpleDateFormat format) {
397-
return milliseconds2String(getCurTimeMills(), format);
411+
return date2String(new Date(), format);
398412
}
399413

400414
/**

utilcode/src/main/java/com/blankj/utilcode/utils/UnclassifiedUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.ActivityManager.RunningServiceInfo;
55
import android.content.ComponentName;
66
import android.content.Context;
7+
import android.util.Base64;
78

89
import java.util.List;
910

0 commit comments

Comments
 (0)