FormatUtil.java
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.xdy.util;
import android.text.TextUtils;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
/**
* @author Administrator
* @version 1.0
* @file FormatUtil.java
* @brief 用于格式化操作,数字转String,日期转String等等
* @date 2017/6/11
* Copyright (c) 2017
* All rights reserved.
*/
public class FormatUtil {
//千位分隔符加两位小数模板
public static final String KILO_SPLIT_AND_DECIMAL = ",###,##0.00";
/**
* double格式化字符串,默认格式化为2位小数
* @param d
* @return
*/
public static String doubleToString(double d) {
return doubleToString(d, "0.00");
}
/**
* exam : #,##0.00 千位分隔符加上保留两位小数 0.0 只保留一位
* @param format
* @return
*/
public static String doubleToString(double d, String format) {
String result = null;
DecimalFormat df = null;
try {
df = new DecimalFormat(format);
} catch (Exception e) {
e.printStackTrace();
df = new DecimalFormat(KILO_SPLIT_AND_DECIMAL);
}
result = df.format(d);
return result;
}
/**
* @param timeStamp 字符串类型的毫秒值
* @return
* @brief 格式毫秒值 成20XX-X-X格式
*/
public static String formatData(String timeStamp) {
if (TextUtils.isEmpty(timeStamp)) {
return "";
}
long ms = Long.valueOf(timeStamp);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(ms);
}
public static String formatKb2MB(long size) {
double i = Double.valueOf(size) / 1024;
String s = doubleToString(i);
return s;
}
/**
* @param phone
* @return
* @brief 处理手机号 , 示例:134****234
*/
public static String disposePhone(String phone) {
return disposeStringByChar(phone, 11, 3, 3);
}
/**
* @param phone
* @return
* @brief 处理身份证 , 示例:450****2134
*/
public static String disposeIdentityCard(String phone) {
return disposeStringByChar(phone, 18, 3, 4);
}
/**
* 处理字符串,
* @param phone
* @param validateLength
* @param beginIndex 距离前面索引
* @param endIndex 距离后面个数
* @return
*/
public static String disposeStringByChar(String phone, int validateLength, int beginIndex, int endIndex) {
String str = null;
if (phone.length() == validateLength) {
String preSuffix = phone.substring(0, beginIndex);
String lastSuffix = phone.substring(phone.length() - endIndex, phone.length());
str = preSuffix + "*****" + lastSuffix;
}
return str;
}
}