11package com .blankj .utilcode .utils ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
45import android .content .res .Resources ;
56import android .graphics .Bitmap ;
1617import java .io .InputStream ;
1718import java .io .OutputStream ;
1819import java .io .UnsupportedEncodingException ;
20+ import java .text .Format ;
21+ import java .util .Date ;
1922import java .util .Locale ;
2023
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-
2624/**
2725 * <pre>
2826 * author: Blankj
@@ -149,13 +147,13 @@ public static double byte2Size(long byteNum, ConstUtils.MemoryUnit unit) {
149147 switch (unit ) {
150148 default :
151149 case BYTE :
152- return (double ) byteNum / BYTE ;
150+ return (double ) byteNum / ConstUtils . BYTE ;
153151 case KB :
154- return (double ) byteNum / KB ;
152+ return (double ) byteNum / ConstUtils . KB ;
155153 case MB :
156- return (double ) byteNum / MB ;
154+ return (double ) byteNum / ConstUtils . MB ;
157155 case GB :
158- return (double ) byteNum / GB ;
156+ return (double ) byteNum / ConstUtils . GB ;
159157 }
160158 }
161159
@@ -177,34 +175,58 @@ public static long size2Byte(long size, ConstUtils.MemoryUnit unit) {
177175 switch (unit ) {
178176 default :
179177 case BYTE :
180- return size * BYTE ;
178+ return size ;
181179 case KB :
182- return size * KB ;
180+ return size * ConstUtils . KB ;
183181 case MB :
184- return size * MB ;
182+ return size * ConstUtils . MB ;
185183 case GB :
186- return size * GB ;
184+ return size * ConstUtils . GB ;
187185 }
188186 }
189187
190188 /**
191- * 字节数转合适大小
189+ * 字节数转合适内存大小
192190 * <p>保留3位小数</p>
193191 *
194192 * @param byteNum 字节数
195- * @return 1...1024 unit
193+ * @return 合适内存大小
196194 */
195+ @ SuppressLint ("DefaultLocale" )
197196 public static String byte2FitSize (long byteNum ) {
198197 if (byteNum < 0 ) {
199198 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 );
206228 } else {
207- return String .format (Locale . getDefault (), "%.3fGB " , ( double ) byteNum / GB );
229+ return String .format ("%d天 " , millis / ConstUtils . DAY );
208230 }
209231 }
210232
@@ -260,9 +282,9 @@ public static ByteArrayOutputStream input2OutputStream(InputStream is) {
260282 if (is == null ) return null ;
261283 try {
262284 ByteArrayOutputStream os = new ByteArrayOutputStream ();
263- byte [] b = new byte [KB ];
285+ byte [] b = new byte [ConstUtils . KB ];
264286 int len ;
265- while ((len = is .read (b , 0 , KB )) != -1 ) {
287+ while ((len = is .read (b , 0 , ConstUtils . KB )) != -1 ) {
266288 os .write (b , 0 , len );
267289 }
268290 return os ;
0 commit comments