Blame view
app/src/main/java/com/dinhcv/lifelogpedometer/utils/Utils.java
10.4 KB
7f095a929
|
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 252 253 254 255 256 |
package com.dinhcv.lifelogpedometer.utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Build; import com.dinhcv.lifelogpedometer.LifeLogApplication; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Utils { private Utils() { } /** * Get color wrapper * * @param context * @param id * @return: */ public static int getColorWrapper(Context context, int id) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return context.getColor(id); } else { //noinspection deprecation return context.getResources().getColor(id); } } /** * Get drawable wrapper * * @param context * @param id * @return: */ public static Drawable getDrawableWrapper(Context context, int id) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return context.getDrawable(id); } else { //noinspection deprecation return context.getResources().getDrawable(id); } } /** * Convert value to string * * @param value * @return */ public static String convert2String2Decimal(double value) { String valueStr = null; DecimalFormat dFormat = new DecimalFormat("####,###,##0.00"); valueStr = dFormat.format(value); return valueStr; } /** * Convert value to string * * @param value * @return */ public static String convert2StringAroundNum(double value) { String valueStr = null; DecimalFormat dFormat = new DecimalFormat("####,###,###"); valueStr = dFormat.format(value); return valueStr; } public static String convertDateToStringDialogSelect(Date input) { //昭和yyyy年MM月dd日 SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); format.setTimeZone(TimeZone.getTimeZone("GMT+07:00")); return format.format(input); } /** * * Convert date to string with format date month year * * @param date date * @return date string */ public static String dateToStringFormatDayMonthYearJp(Date date) { String dateStr = null; if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日", Locale.JAPAN); dateStr = sdf.format(date); } return dateStr; } /** * * Convert date to string with format date month year * * @param date date * @return date string */ public static String dateToStringFormatDayMonthYear(Date date) { String dateStr = null; if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN); dateStr = sdf.format(date); } return dateStr; } /** * * Convert date to string with format date month year * @param date date * @return date string */ public static String dateToStringFormatYearMonthDay(Date date) { String dateStr = null; if ( date != null ) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.JAPAN); dateStr = sdf.format(date); } Debug.normal("DATE format: "+dateStr); return dateStr; } public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { long diffInMillies = date2.getTime() - date1.getTime(); return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); } public static boolean checkMailFormat(String text) { String regex = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); if (matcher.matches()) { Debug.normal("String is Full width character"); return true; } else { Debug.normal("String is not Full width character"); return false; } } public static long getToday() { Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTimeInMillis(); } /** * @return milliseconds since 1.1.1970 for tomorrow 0:00:01 local timezone */ public static long getTomorrow() { Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 1); c.set(Calendar.MILLISECOND, 0); c.add(Calendar.DATE, 1); return c.getTimeInMillis(); } public static long getStandarDate(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTimeInMillis(); } public static Date getFromDate(Date date, Const.DATA_TYPE dataType ) { Calendar c = Calendar.getInstance(); c.setTime(date); if (dataType == Const.DATA_TYPE.ONE_DAY){ c.add(Calendar.DAY_OF_YEAR, -1); }else if (dataType == Const.DATA_TYPE.ONE_WEEK){ c.add(Calendar.DAY_OF_YEAR, -7); }else if (dataType == Const.DATA_TYPE.ONE_MONTH){ c.add(Calendar.DAY_OF_YEAR, -30); }else if (dataType == Const.DATA_TYPE.THREE_MONTH){ c.add(Calendar.DAY_OF_YEAR, - 30 * 3); }else if (dataType == Const.DATA_TYPE.SIX_MONTH){ c.add(Calendar.DAY_OF_YEAR, - 30 * 6); } return c.getTime(); } public static int getMonth(Date date) { int month = 0; if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat("MM", Locale.JAPAN); String dateStr = sdf.format(date); if (dateStr != null) month = Integer.valueOf(dateStr); } Debug.normal("Month: " + month); return month; } public static int getDay(Date date) { int month = 0; if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat("dd", Locale.JAPAN); String dateStr = sdf.format(date); if (dateStr != null) month = Integer.valueOf(dateStr); } Debug.normal("Month: " + month); return month; } public static String userAgent = null; public static void setCustomUA(String ua) { userAgent = ua; } public static String getCustomUA() { Context context = LifeLogApplication.context; String packageName = context.getPackageName(); //{2} String appVersion = "Android" + "." + "1.0.1"; //{3} String osVersion = Build.VERSION.RELEASE; //{4} String modelName = Build.MODEL; //{6} String uaString = packageName + "/" + appVersion + "(Android " + osVersion + ";" + modelName + ")" + " " + userAgent; return uaString; } public static Date convertString2Date(String time) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = df.parse(time); } catch (ParseException e) { e.printStackTrace(); } return date; } |
86ee629d1
|
257 258 259 260 |
public static String convertDate2DateTimeString(Date date) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.format(date); } |
7f095a929
|
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
public static String convertDate2DayString(Date date) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(date); } public static String convertDate2TimeString(Date date) { DateFormat df = new SimpleDateFormat("HH:mm"); return df.format(date); } public static String convertTimeStringFromString(int time){ String timeCv = null; String timeStr = String.valueOf(time); int length = timeStr.length(); switch (length){ case 0: timeCv = "00:00:00"; break; case 1: timeCv = "00:00:0"+time; break; case 2: timeCv = "00:00:"+time; break; case 3: timeCv = "00:0"+timeStr.substring(0, 1)+":"+ timeStr.substring(1, timeStr.length()); break; case 4: timeCv = "00:"+timeStr.substring(0, 2)+":"+ timeStr.substring(2, timeStr.length()); break; case 5: timeCv = timeStr.substring(0, 1)+ ":"+timeStr.substring(1, 3)+":"+ timeStr.substring(3, timeStr.length()); break; case 6: timeCv = timeStr.substring(0, 2)+":"+timeStr.substring(2, 4)+":"+ timeStr.substring(4, timeStr.length()); break; default: timeCv = timeStr; break; } return timeCv; } public static String convertSecond2HourMinSecString(int total){ int hours = total / 3600; int minutes = (total % 3600) / 60; int seconds = total % 60; return String.format("%02d:%02d:%02d", hours, minutes, seconds); } public static String formatInt2LengthDefault(int value){ DecimalFormat dFormat = new DecimalFormat("00"); String data = dFormat.format(value); return data; } public static String getWeekdayFromDate(Date date) { SimpleDateFormat outFormat = new SimpleDateFormat("EEEE", Locale.JAPAN); String goal = outFormat.format(date); Debug.normal("WEEKDAY: " + goal); return goal; } public static int parseString2Int(String input, int defaul) { try { return Integer.parseInt(input); } catch (NumberFormatException e) { return defaul; } } } /****************************************************************************** * End of file *****************************************************************************/ |