-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFormatUtil.java
More file actions
executable file
·251 lines (188 loc) · 7.58 KB
/
TextFormatUtil.java
File metadata and controls
executable file
·251 lines (188 loc) · 7.58 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.example.utils;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.style.ForegroundColorSpan;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import example.base.util.LogUtil;
/**
* Created by Silver on 24/10/2016.
*/
public class TextFormatUtil {
private static SimpleDateFormat mDateFormat;
private static SimpleDateFormat timeFormat;
private static SimpleDateFormat dayFormat;
private static NumberFormat mNumberFormat;
public static SimpleDateFormat getDateFormat() {
if (mDateFormat == null) {
mDateFormat = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
}
return mDateFormat;
}
public static SimpleDateFormat getTimeFormat() {
if (timeFormat == null) {
timeFormat = new SimpleDateFormat("HH:mm a", Locale.ENGLISH);
}
return timeFormat;
}
public static SimpleDateFormat getDayFormat() {
if (dayFormat == null) {
dayFormat = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
}
return dayFormat;
}
public static NumberFormat getNumberFormat() {
if (mNumberFormat == null) {
mNumberFormat = new DecimalFormat();
}
return mNumberFormat;
}
public static void replace(List<String> strings) {
ListIterator<String> iterator = strings.listIterator();
while (iterator.hasNext()) {
iterator.set(iterator.next().toUpperCase());
}
}
public static String carPlate(String plate) {
StringBuilder builder = new StringBuilder();
builder.append(plate != null ? plate : "N.A");
return builder.toString();
}
public static String carMileage(Integer mileage) {
StringBuilder builder = new StringBuilder();
builder.append(mileage != null ? getNumberFormat().format(mileage) : "N.A").append(" km");
return builder.toString();
}
public static String carRegDate(Date regDate) {
StringBuilder builder = new StringBuilder();
builder.append(regDate != null ? getDateFormat().format(regDate) : "N.A");
return builder.toString();
}
public static String status(String status) {
StringBuilder builder = new StringBuilder();
builder.append(status);
return builder.toString();
}
public static String getQuotedText(Context context, String carPlate) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(context.getString(R.string.quote_desc_1));
builder.append(" <b>");
SpannableString redSpannable = new SpannableString(carPlate);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, carPlate.length(), 0);
builder.append(redSpannable);
builder.append("</b> ");
builder.append(context.getString(R.string.quote_recently));
return builder.toString();
}
public static String getActiveAuctionText(Context context, String carPlate) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(context.getString(R.string.quote_active_listing1));
builder.append(" <b>");
SpannableString redSpannable = new SpannableString(carPlate);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, carPlate.length(), 0);
builder.append(redSpannable);
builder.append("</b> ");
builder.append(context.getString(R.string.quote_active_listing2));
return builder.toString();
}
public static String getNobodyQuote(Context context, int quoteAmount) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(context.getString(R.string.quote_nobody));
builder.append(" <b'>");
String currency = getNumberInCurrency(quoteAmount);
SpannableString redSpannable = new SpannableString(currency);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, currency.length(), 0);
builder.append(redSpannable);
builder.append("</b>.");
return builder.toString();
}
public static String getNobodyQuoteShort(Context context, String carPlate) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(context.getString(R.string.quote_nobody_short));
builder.append(" <b'>");
SpannableString redSpannable = new SpannableString(carPlate);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, carPlate.length(), 0);
builder.append(redSpannable);
builder.append("</b>.");
return builder.toString();
}
public static String carPlateDot(String carPlate) {
StringBuilder builder = new StringBuilder();
if (!TextUtils.isEmpty(carPlate)) {
builder.append(carPlate);
builder.append(" ");
}
return builder.toString();
}
public static String mileageDot(String mileage) {
StringBuilder builder = new StringBuilder();
if (!TextUtils.isEmpty(mileage)) {
builder.append(getNumberFormat().format(Double.parseDouble(mileage)));
builder.append(" km");
}
return builder.toString();
}
public static String getNumberInCurrency(int number) {
String price = "";
try {
String symbol = "$";
price = symbol + getNumberFormat().format(number);
} catch (Exception ex) {
LogUtil.e(ex);
}
return price;
}
public static String getNumberInCurrency(Double number) {
String price = "";
try {
String symbol = "$";
price = symbol + getNumberFormat().format(number);
} catch (Exception ex) {
LogUtil.e(ex);
}
return price;
}
private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
}
private static String format(long value) {
//Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
if (value == Long.MIN_VALUE) {
return format(Long.MIN_VALUE + 1);
}
if (value < 0) {
return "-" + format(-value);
}
if (value < 1000) {
return Long.toString(value); //deal with easy case
}
Map.Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();
long truncated = value / (divideBy / 10); //the number part of the output times 10
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}
public static String getTimeDifferences(Date calen) {
if (new Date().after(calen)) {
return new StringBuilder()
.append("Expired").toString();
} else {
return new StringBuilder()
.append(DateUtils.getRelativeTimeSpanString(calen.getTime(),
Calendar.getInstance().getTimeInMillis(), DateUtils.MINUTE_IN_MILLIS)).toString();
}
}
}