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; } public static String convertDate2DateTimeString(Date date) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.format(date); } 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 *****************************************************************************/