common methods for date,time

package com.market_place.car_products.utils import android.app.Activity import android.app.DatePickerDialog import android.content.Context import android.graphics.Color import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.inputmethod.InputMethodManager import androidx.appcompat.app.AlertDialog import androidx.databinding.ObservableField import com.google.firebase.Timestamp import com.market_place.car_products.databinding.AlertMessageBinding import com.market_place.car_products.databinding.IssueTitleBinding import java.text.ParseException import java.text.SimpleDateFormat import java.util.* import java.util.regex.Matcher import java.util.regex.Pattern object CommonMethods { var alertRespDialog: AlertDialog? = null fun hideSoftKeyBoard(activity: Activity) { val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager var view = activity.currentFocus try { view?.clearFocus() } catch (e: Exception) { e.printStackTrace() } if (view == null) { view = View(activity) } else { inputMethodManager.hideSoftInputFromWindow( view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS ) } } /** Email Validator */ fun isEmailValid(string: String): Boolean { return Pattern.compile( "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z]{2,5}" + ")+" ).matcher(string.trim()) .matches() } fun isNameValid(string: String): Boolean { return Pattern.compile( "[A-Za-z]+(\\s[A-Za-z]+)?" ).matcher(string.trim()) .matches() } fun validPassword(password: String): Boolean { val pattern: Pattern val matcher: Matcher val PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[@*#$%^&+=!])(?=\\S+$).{4,}$" pattern = Pattern.compile(PASSWORD_PATTERN) matcher = pattern.matcher(password) return matcher.matches() } //mobile validator fun isMobileValid(string: String): Boolean { return Pattern.compile("([0-9]{7,15})") .matcher(string.trim()) .matches() } fun isStateValid(string: String): Boolean { return Pattern.compile(" \"([a - zA - Z] + |[a - zA - Z] + \\\\s[a - zA - Z] + )\"") .matcher(string.trim()) .matches() } fun isCityValid(string: String): Boolean { return Pattern.compile(" \"([a - zA - Z] + |[a - zA - Z] + \\\\s[a - zA - Z] + )\"") .matcher(string.trim()) .matches() } fun isStreetno(string: String): Boolean { return Pattern.compile(" ^[#.0-9a-zA-Z\\s,-]+\$") .matcher(string.trim()) .matches() } fun isPincode(string: String): Boolean { return Pattern.compile(" ^[1-9][0-9]{5}\$") .matcher(string.trim()) .matches() } fun isNameValidss(string: String): Boolean { return Pattern.compile( "[A-Za-z]+(\\s[A-Za-z]+)?+(\\s[A-Za-z]+)?+(\\s[A-Za-z]+)?+(\\s[A-Za-z]+)?+(\\s[A-Za-z]+)?+(\\s[A-Za-z]+)?" ).matcher(string.trim()) .matches() } fun alertResponseMessage(context: Context, message: String) { if (alertRespDialog != null) { alertRespDialog!!.dismiss() } val binding = AlertMessageBinding.inflate(LayoutInflater.from(context), null, false) val builder = AlertDialog.Builder(context) builder.setView(binding.root) builder.setCancelable(false) alertRespDialog = builder.create() alertRespDialog?.window!!.decorView.setBackgroundColor(Color.TRANSPARENT) if (!alertRespDialog!!.isShowing) { alertRespDialog?.show() } binding.tvMessage.text = message binding.tvOk.setOnClickListener { alertRespDialog?.dismiss() } } fun alertNewResponseMessage(context: Context, message: String) { if (alertRespDialog != null) { alertRespDialog!!.dismiss() } val binding = IssueTitleBinding.inflate(LayoutInflater.from(context), null, false) val builder = AlertDialog.Builder(context) builder.setView(binding.root) builder.setCancelable(false) alertRespDialog = builder.create() alertRespDialog?.window!!.decorView.setBackgroundColor(Color.TRANSPARENT) if (!alertRespDialog!!.isShowing) { alertRespDialog?.show() } binding.message.text = message binding.cancelAction.setOnClickListener { alertRespDialog?.dismiss() } binding.action.setOnClickListener { alertRespDialog?.dismiss() } } fun openCalendarAndSelectDate( context: Context, fromDate: ObservableField, outputDate: SimpleDateFormat, isMinDate: Boolean ) { hideSoftKeyBoard(context as Activity) val myCalendar = Calendar.getInstance() val date = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> myCalendar.set(Calendar.YEAR, year) myCalendar.set(Calendar.MONTH, monthOfYear) myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth) fromDate.set(outputDate.format(myCalendar.time)) } val datePick = DatePickerDialog( context,DatePickerDialog.THEME_HOLO_LIGHT, date, myCalendar[Calendar.YEAR], myCalendar[Calendar.MONTH], myCalendar[Calendar.DAY_OF_MONTH] ) if (isMinDate) { datePick.datePicker.minDate = Date().time } datePick.show() } fun getTime(time: String): String { var str:String?=null val sdf = SimpleDateFormat("hh:mm:ss") val sdfs = SimpleDateFormat("hh:mm") var d: Date? = null try { d = sdf.parse(time) } catch (e: ParseException) { e.printStackTrace() } str=sdfs.format(d) return str } fun getDateFormat(stringDate: String): String? { var string: String? = null val input = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") val output = SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH) var d: Date? = null try { d = input.parse(stringDate) } catch (e: ParseException) { e.printStackTrace() } string = output.format(d) Log.e("DateFormatting==",string) return string } fun timeStampToHeaderDate(timestamp: Timestamp?): String { var time = "" val sdf = SimpleDateFormat( "EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH ) val now = System.currentTimeMillis() val till = (sdf.parse(timestamp?.toDate().toString()) as Date).time val output = SimpleDateFormat("MMM dd,yy", Locale.ENGLISH) sdf.timeZone = TimeZone.getTimeZone("India") val millis: Long = now - till val hours = (millis / (1000 * 60 * 60)).toInt() val currentCal = Calendar.getInstance() val serverCalendar = Calendar.getInstance() serverCalendar.timeInMillis = till Log.e("Hours","$hours ${currentCal[Calendar.DAY_OF_MONTH]} ${serverCalendar[Calendar.DAY_OF_MONTH]}") val currentDay = currentCal[Calendar.DAY_OF_MONTH] val serverDay = serverCalendar[Calendar.DAY_OF_MONTH] val dayDiff = currentDay - serverDay time = if (hours > 24 || currentDay != serverDay) { if (dayDiff==1) { "Yesterday" } else { output.format(sdf.parse(timestamp?.toDate().toString())) ?: "" } } else { "Today" } return time } fun timeStampToTime(timestamp: Timestamp?): String { var time = "" val sdf = SimpleDateFormat( "EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH ) val now = System.currentTimeMillis() val till = (sdf.parse(timestamp?.toDate().toString()) as Date).time val output = SimpleDateFormat("hh:mm aa", Locale.ENGLISH) sdf.timeZone = TimeZone.getTimeZone("India") val millis: Long = now - till val hours = (millis / (1000 * 60 * 60)).toInt() return output.format(sdf.parse(timestamp?.toDate().toString())) ?: "" } fun checkLoginOrNot(context: Context): Boolean { return PreferenceFile.retrieveKey(context, CommonKeys.USER_LOGGED) == "yes" } }

Comments