1
1
package com .blankj .utilcode .utils ;
2
2
3
+ import android .annotation .SuppressLint ;
3
4
import android .content .Context ;
4
5
import android .content .res .Resources ;
5
6
import android .graphics .Bitmap ;
16
17
import java .io .InputStream ;
17
18
import java .io .OutputStream ;
18
19
import java .io .UnsupportedEncodingException ;
20
+ import java .text .Format ;
21
+ import java .util .Date ;
19
22
import java .util .Locale ;
20
23
21
- import static com .blankj .utilcode .utils .ConstUtils .BYTE ;
22
- import static com .blankj .utilcode .utils .ConstUtils .GB ;
23
- import static com .blankj .utilcode .utils .ConstUtils .KB ;
24
- import static com .blankj .utilcode .utils .ConstUtils .MB ;
25
-
26
24
/**
27
25
* <pre>
28
26
* author: Blankj
@@ -149,13 +147,13 @@ public static double byte2Size(long byteNum, ConstUtils.MemoryUnit unit) {
149
147
switch (unit ) {
150
148
default :
151
149
case BYTE :
152
- return (double ) byteNum / BYTE ;
150
+ return (double ) byteNum / ConstUtils . BYTE ;
153
151
case KB :
154
- return (double ) byteNum / KB ;
152
+ return (double ) byteNum / ConstUtils . KB ;
155
153
case MB :
156
- return (double ) byteNum / MB ;
154
+ return (double ) byteNum / ConstUtils . MB ;
157
155
case GB :
158
- return (double ) byteNum / GB ;
156
+ return (double ) byteNum / ConstUtils . GB ;
159
157
}
160
158
}
161
159
@@ -177,34 +175,58 @@ public static long size2Byte(long size, ConstUtils.MemoryUnit unit) {
177
175
switch (unit ) {
178
176
default :
179
177
case BYTE :
180
- return size * BYTE ;
178
+ return size ;
181
179
case KB :
182
- return size * KB ;
180
+ return size * ConstUtils . KB ;
183
181
case MB :
184
- return size * MB ;
182
+ return size * ConstUtils . MB ;
185
183
case GB :
186
- return size * GB ;
184
+ return size * ConstUtils . GB ;
187
185
}
188
186
}
189
187
190
188
/**
191
- * 字节数转合适大小
189
+ * 字节数转合适内存大小
192
190
* <p>保留3位小数</p>
193
191
*
194
192
* @param byteNum 字节数
195
- * @return 1...1024 unit
193
+ * @return 合适内存大小
196
194
*/
195
+ @ SuppressLint ("DefaultLocale" )
197
196
public static String byte2FitSize (long byteNum ) {
198
197
if (byteNum < 0 ) {
199
198
return "shouldn't be less than zero!" ;
200
- } else if (byteNum < KB ) {
201
- return String .format (Locale .getDefault (), "%.3fB" , (double ) byteNum );
202
- } else if (byteNum < MB ) {
203
- return String .format (Locale .getDefault (), "%.3fKB" , (double ) byteNum / KB );
204
- } else if (byteNum < GB ) {
205
- return String .format (Locale .getDefault (), "%.3fMB" , (double ) byteNum / MB );
199
+ } else if (byteNum < ConstUtils .KB ) {
200
+ return String .format ("%.3fB" , (double ) byteNum );
201
+ } else if (byteNum < ConstUtils .MB ) {
202
+ return String .format ("%.3fKB" , (double ) byteNum / ConstUtils .KB );
203
+ } else if (byteNum < ConstUtils .GB ) {
204
+ return String .format ("%.3fMB" , (double ) byteNum / ConstUtils .MB );
205
+ } else {
206
+ return String .format ("%.3fGB" , (double ) byteNum / ConstUtils .GB );
207
+ }
208
+ }
209
+
210
+ /**
211
+ * 毫秒时间戳转合适时间长度
212
+ *
213
+ * @param millis 毫秒时间戳
214
+ * @return 合适时间长度
215
+ */
216
+ @ SuppressLint ("DefaultLocale" )
217
+ public static String millis2FitTimeSpan (long millis ) {
218
+ if (millis < 0 ) {
219
+ return "shouldn't be less than zero!" ;
220
+ } else if (millis < ConstUtils .SEC ) {
221
+ return String .format ("%d毫秒" , millis );
222
+ } else if (millis < ConstUtils .MIN ) {
223
+ return String .format ("%d秒" , millis / ConstUtils .SEC );
224
+ } else if (millis < ConstUtils .HOUR ) {
225
+ return String .format ("%d分" , millis / ConstUtils .MIN );
226
+ } else if (millis < ConstUtils .DAY ) {
227
+ return String .format ("%d小时" , millis / ConstUtils .HOUR );
206
228
} else {
207
- return String .format (Locale . getDefault (), "%.3fGB " , ( double ) byteNum / GB );
229
+ return String .format ("%d天 " , millis / ConstUtils . DAY );
208
230
}
209
231
}
210
232
@@ -260,9 +282,9 @@ public static ByteArrayOutputStream input2OutputStream(InputStream is) {
260
282
if (is == null ) return null ;
261
283
try {
262
284
ByteArrayOutputStream os = new ByteArrayOutputStream ();
263
- byte [] b = new byte [KB ];
285
+ byte [] b = new byte [ConstUtils . KB ];
264
286
int len ;
265
- while ((len = is .read (b , 0 , KB )) != -1 ) {
287
+ while ((len = is .read (b , 0 , ConstUtils . KB )) != -1 ) {
266
288
os .write (b , 0 , len );
267
289
}
268
290
return os ;
0 commit comments