Created
April 21, 2018 12:10
-
-
Save vishalhalani/af1e38367dc9296b450c095b1baf41b0 to your computer and use it in GitHub Desktop.
Useful function of Date Time,Snackbar,Internet Connection checking,Json String Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ********************************************* SHOW SNACKBAR METHOD ******************************************************** | |
public static void showSnackbar(String msg, View v, boolean isError) { | |
Snackbar snackbar = Snackbar.make(v, "" + msg | |
, Snackbar.LENGTH_LONG); | |
if (isError) { | |
View sbView = snackbar.getView(); | |
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); | |
textView.setTextColor(Color.RED); | |
textView.setTextSize(20); | |
snackbar.show(); | |
} else { | |
View sbView = snackbar.getView(); | |
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); | |
textView.setTextColor(Color.WHITE); | |
textView.setTextSize(20); | |
snackbar.show(); | |
} | |
} | |
// ********************************************* INTERNET CONNECTIVITY CHECK METHOD ******************************************************** | |
/** | |
* @param activity : Contain activity | |
* @return boolean : True or false | |
* @description : Check data connection is on or not | |
*/ | |
public static final boolean isInternetOn(Activity activity) { | |
// get Connectivity Manager object to check connection | |
ConnectivityManager connec; | |
connec = (ConnectivityManager) activity.getSystemService(Activity.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = null; | |
if (connec != null) { | |
activeNetwork = connec.getActiveNetworkInfo(); | |
} | |
// Check for network connections | |
if (activeNetwork != null) { | |
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { | |
if (activeNetwork.getState() == NetworkInfo.State.CONNECTED) { | |
// if connected with internet | |
return true; | |
} else if (activeNetwork.getState() == NetworkInfo.State.CONNECTING) { | |
// connected to the mobile provider's data plan | |
return true; | |
} | |
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE ) { | |
if (activeNetwork.getState() == NetworkInfo.State.CONNECTED) { | |
// connected to the mobile provider's data plan | |
return true; | |
} else if (activeNetwork.getState() == NetworkInfo.State.CONNECTING) { | |
// connected to the mobile provider's data plan | |
return true; | |
} | |
} | |
else if ( activeNetwork.getType() == ConnectivityManager.TYPE_ETHERNET ) { | |
if (activeNetwork.getState() == NetworkInfo.State.CONNECTED) { | |
// connected to the mobile provider's data plan | |
return true; | |
} else if (activeNetwork.getState() == NetworkInfo.State.CONNECTING) { | |
// connected to the mobile provider's data plan | |
return true; | |
} | |
} | |
} else { | |
// buildDialog(activity); | |
// Toast.makeText(activity, "Internet Not Available ", Toast.LENGTH_LONG).show(); | |
return false; | |
} | |
return false; | |
} | |
// ************************************* VALIDATION FUNCTIONS **************************************************************** | |
public boolean isValidEmail(String email) { | |
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" | |
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
Pattern pattern = Pattern.compile(EMAIL_PATTERN); | |
Matcher matcher = pattern.matcher(email); | |
return matcher.matches(); | |
} | |
// validating Mobile number | |
public static boolean isValidMobile(String mobile) { | |
boolean check = false; | |
if (!Pattern.matches("[a-zA-Z]+", mobile)) { | |
if (mobile.length() > 9 && mobile.length() < 11) { | |
check = true; | |
} else { | |
check = false; | |
} | |
} else { | |
check = false; | |
} | |
return check; | |
} | |
public static boolean isValidPAN(String pan, char ch1, char ch2) { | |
boolean isValidPan = true; | |
String Pan = pan; | |
if (Pan.matches(("[A-Z]{3}([CHFATBLJGP])(?:(?<=P)" + ch1 + "|(?<!P)" + ch2 + ")[0-9]{4}[A-Z]"))) { | |
isValidPan = true; | |
} else { | |
isValidPan = false; | |
} | |
return isValidPan; | |
} | |
// validating name | |
public boolean isNotOnlySpace(String text) { | |
if (text != null && text.trim().length() > 0) { | |
return true; | |
} | |
return false; | |
} | |
public boolean isAlphabates(String s) { | |
Pattern ps = Pattern.compile("^[a-zA-Z ]+$"); | |
Matcher ms = ps.matcher(s); | |
return ms.matches(); | |
} | |
public boolean isValidwebsite(String txtWebsite) { | |
return Patterns.WEB_URL.matcher(txtWebsite).matches(); | |
} | |
public static String escapeStrings(String s) { | |
String result = null; | |
try { | |
result = s.replace("'", "\\'"); | |
} | |
// This exception should never occur. | |
catch (Exception e) { | |
result = s; | |
} | |
return result; | |
} | |
// ************************************** DATE TIME FUNCTIONS ***************************************************************** | |
public String convertDateToUserTimeZone(String serverDate) { | |
String ourdate; | |
try { | |
SimpleDateFormat formatter = new SimpleDateFormat(serverdateFormat, Locale.UK); | |
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); | |
Date value = formatter.parse(serverDate); | |
TimeZone timeZone = TimeZone.getTimeZone("Asia/Kolkata"); | |
SimpleDateFormat dateFormatter = new SimpleDateFormat(serverdateFormat, Locale.UK); //this format changeable | |
dateFormatter.setTimeZone(timeZone); | |
ourdate = dateFormatter.format(value); | |
//Log.d("OurDate", OurDate); | |
} catch (Exception e) { | |
ourdate = "00-00-0000 00:00"; | |
} | |
return ourdate; | |
} | |
public Date convertUTCDateToUserTimeZoneDate(String date) { | |
Date date2 = null; | |
SimpleDateFormat df1 = new SimpleDateFormat(serverdateFormat, Locale.UK); | |
df1.setTimeZone(TimeZone.getTimeZone(this.timeZone)); | |
try { | |
date2 = df1.parse(date); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return date2; | |
} | |
public String convertToServerDate(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat(userdateFormat, Locale.ENGLISH); | |
Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
return serverFormat.format(d); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public String convertToServerDate(Date date) { | |
try { | |
// SimpleDateFormat format = new SimpleDateFormat(userdateFormat, Locale.ENGLISH); | |
// Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.UK); | |
return serverFormat.format(date); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public int getAge(int _year, int _month, int _day) { | |
GregorianCalendar cal = new GregorianCalendar(); | |
int y, m, d, a; | |
y = cal.get(Calendar.YEAR); | |
m = cal.get(Calendar.MONTH); | |
d = cal.get(Calendar.DAY_OF_MONTH); | |
cal.set(_year, _month, _day); | |
a = y - cal.get(Calendar.YEAR); | |
if ((m < cal.get(Calendar.MONTH)) | |
|| ((m == cal.get(Calendar.MONTH)) && (d < cal | |
.get(Calendar.DAY_OF_MONTH)))) { | |
--a; | |
} | |
if (a < 0) | |
throw new IllegalArgumentException("Age < 0"); | |
return a; | |
} | |
// Used to convert 24hr format to 12hr format with AM/PM values | |
public String convertTimeTo12Hours(int hours, int mins) { | |
String timeSet = ""; | |
if (hours > 12) { | |
hours -= 12; | |
timeSet = "PM"; | |
} else if (hours == 0) { | |
hours += 12; | |
timeSet = "AM"; | |
} else if (hours == 12) | |
timeSet = "PM"; | |
else | |
timeSet = "AM"; | |
String minutes = ""; | |
if (mins < 10) | |
minutes = "0" + mins; | |
else | |
minutes = String.valueOf(mins); | |
// Append in a StringBuilder | |
String aTime = new StringBuilder().append(hours).append(':') | |
.append(minutes).append(" ").append(timeSet).toString(); | |
return aTime; | |
} | |
public int getHoursFromDateTime(String date) { | |
int hours = 0; | |
SimpleDateFormat format = new SimpleDateFormat(serverdateFormat, Locale.ENGLISH); | |
try { | |
Date d = format.parse(date); | |
SimpleDateFormat sdf = new SimpleDateFormat("HH", Locale.UK); | |
hours = Integer.parseInt(sdf.format(d)); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return hours; | |
} | |
public int getMinutesFromDateTime(String date) { | |
int min = 0; | |
SimpleDateFormat format = new SimpleDateFormat(serverdateFormat, Locale.ENGLISH); | |
try { | |
Date d = format.parse(date); | |
SimpleDateFormat sdf = new SimpleDateFormat("mm", Locale.UK); | |
min = Integer.parseInt(sdf.format(d)); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return min; | |
} | |
public boolean isBetweenDates(Date start, Date end, Date compare) { | |
if (compare.compareTo(start) >= 0 && compare.compareTo(end) <= 0) { | |
return true; | |
} | |
return false; | |
} | |
public Date convertToDate(String dateString) { | |
SimpleDateFormat dateFormat = new SimpleDateFormat(userdateFormat, Locale.UK); | |
Date convertedDate = new Date(); | |
try { | |
convertedDate = dateFormat.parse(dateString); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return convertedDate; | |
} | |
public boolean isMatchedDate(Date date, String recorddate) { | |
// Log.i("TAG", "Record DATE=====>" + recorddate); | |
SimpleDateFormat dateFormat = new SimpleDateFormat(userdateFormat, Locale.UK); | |
String weekDate = dateFormat.format(date); | |
String recordDate = convertServerDateToUserDate(recorddate); | |
if (weekDate.equals(recordDate)) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public String convertToUserServerDateToUTCServerDate(String date) { | |
Date date1 = null; | |
SimpleDateFormat df = new SimpleDateFormat(serverdateFormat, Locale.UK); | |
df.setTimeZone(TimeZone.getTimeZone(timeZone)); | |
try { | |
date1 = df.parse(date); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
SimpleDateFormat df1 = new SimpleDateFormat(serverdateFormat, Locale.UK); | |
df1.setTimeZone(TimeZone.getTimeZone("UTC")); | |
String formattedDate = df1.format(date1); | |
// Log.i("TAG", "Result UTC:=> " + formattedDate); | |
return formattedDate; | |
} | |
public String convertServerDateToUserDate(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat( | |
"yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); | |
Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat(userdateFormat, Locale.ENGLISH); | |
return serverFormat.format(d); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public String convertToUserDate(Date date) { | |
try { | |
// SimpleDateFormat format = new SimpleDateFormat(userdateFormat, Locale.ENGLISH); | |
// Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat(userdateFormat, Locale.UK); | |
return serverFormat.format(date); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public Date convertServerDateToDate(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat( | |
"yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); | |
Date d = format.parse(date); | |
return d; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public Date convertUserDateToDate(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat( | |
userdateFormat, Locale.UK); | |
Date d = format.parse(date); | |
return d; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public String convertServerDateToUserFormat(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat(serverdateFormat, Locale.ENGLISH); | |
Date d = (Date) format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat(userdateFormat, Locale.ENGLISH); | |
return serverFormat.format(d); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public String convertServerDateOnly(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat( | |
userdateFormat, Locale.ENGLISH); | |
Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
return serverFormat.format(d); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
public String convertServerDate(String date) { | |
try { | |
SimpleDateFormat format = new SimpleDateFormat( | |
userdateFormat, Locale.ENGLISH); | |
Date d = format.parse(date); | |
SimpleDateFormat serverFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | |
return serverFormat.format(d); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
// ***************************************** CREATE JSON STRING FUNCTION **************************************************** | |
// create array json form value of arraylist | |
public String makeIdJson(final ArrayList<String> idValue) { | |
String idJson = ""; | |
StringBuilder builder = new StringBuilder(); | |
if (idValue != null && idValue.size() > 0) { | |
for (int i = 0; i < idValue.size(); i++) { | |
if (i < idValue.size() - 1) { | |
builder.append(idValue).append(","); | |
} else { | |
builder.append(idValue); | |
} | |
} | |
} | |
idJson = "{'results':" + idValue.toString() + "}"; | |
return idJson; | |
} | |
// create array json form value of array | |
public String makeIdJson(final String[] idValue) { | |
String idJson = ""; | |
StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < idValue.length; i++) { | |
if (i < idValue.length - 1) { | |
builder.append(idValue[i]).append(","); | |
} else { | |
builder.append(idValue[i]); | |
} | |
} | |
idJson = "{'results':[" + builder.toString() + "]}"; | |
return idJson; | |
} | |
// create key value pair json using string and interger values | |
public String getJsonStringWithID(final String[] fieldName, | |
final String[] fieldIdName, final String[] fieldValue, | |
final String[] fieldIdValue, final String listtype) { | |
String json; | |
StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < fieldValue.length; i++) { | |
if (fieldValue[i] != null) { | |
builder.append("'" + fieldName[i] + "':'" + escapeStrings(fieldValue[i]) + "',"); | |
} else { | |
builder.append("'" + fieldName[i] + "':" + escapeStrings(fieldValue[i]) + ","); | |
} | |
} | |
for (int i = 0; i < fieldIdValue.length; i++) { | |
if (i < fieldIdValue.length - 1) { | |
builder.append("'" + fieldIdName[i] + "':" + fieldIdValue[i] | |
+ ","); | |
} else { | |
builder.append("'" + fieldIdName[i] + "':" + fieldIdValue[i]); | |
} | |
} | |
json = "{'__metadata':{'type':'" + listtype + "'}," | |
+ builder.toString() + "}"; | |
return json; | |
} | |
// create key value pair json using string and interger values | |
public String getJsonStringWithSingleID(final String fieldName, | |
final String fieldIdName, final String fieldValue, | |
final String fieldIdValue, final String listtype) { | |
String json; | |
StringBuilder builder = new StringBuilder(); | |
builder.append("'" + fieldName + "':'" + fieldValue + "',"); | |
builder.append("'" + fieldIdName + "':" + fieldIdValue); | |
json = "{'__metadata':{'type':'" + listtype + "'}," | |
+ builder.toString() + "}"; | |
return json; | |
} | |
// create key value pair json using string and interger values | |
public String getJsonString(final String fieldname, | |
final String fieldValue, final String listtype) { | |
return "{'__metadata':{'type':'" + listtype + "'},'" + fieldname | |
+ "':'" + fieldValue + "'}"; | |
} | |
// create key value pair json using string and interger values | |
public String getJsonSingleItemObj(final String fieldname, | |
final String fieldIdValue, final String listtype) { | |
return "{'__metadata':{'type':'" + listtype + "'},'" + escapeStrings(fieldname) | |
+ "':" + fieldIdValue + "}"; | |
} | |
// create json for mulichoice column | |
public String getMultipleChoiceJsonString(ArrayList<String> list) { | |
String idJson = ""; | |
StringBuilder builder = new StringBuilder(); | |
if (list != null) { | |
if (list.size() > 0) { | |
for (int i = 0; i < list.size(); i++) { | |
if (i < list.size() - 1) { | |
builder.append("'" + escapeStrings(list.get(i))).append("',"); | |
} else { | |
builder.append("'" + escapeStrings(list.get(i)) + "'"); | |
} | |
} | |
} | |
} | |
idJson = "'results':[" + builder.toString() + "]"; | |
return "{'__metadata':{'type':'Collection(Edm.String)'}," + idJson + "}"; | |
} | |
//)/AttachmentFiles/ add(FileName='" + fileName + "')", | |
// create upload file json using file pathl | |
public String getJsonStringFileUpload(final String fieldname, | |
final String fieldValue, final String listtype) { | |
return "'AttachmentFiles':{'results':[{'__metadata':{'type':'" + listtype + "'},'" + fieldname | |
+ "':'" + fieldValue + "'}]}"; | |
} | |
// create key value pair json using string and interger values | |
public String getJsonString(final String[] fieldName, | |
final String[] fieldValue, final String listtype) { | |
String json; | |
StringBuilder builder = new StringBuilder(); | |
for (int i = 0; i < fieldValue.length; i++) { | |
if (i < fieldValue.length - 1) { | |
builder.append("'" + fieldName[i] + "':'" + escapeStrings(fieldValue[i]) | |
+ "',"); | |
} else { | |
builder.append("'" + fieldName[i] + "':'" + escapeStrings(fieldValue[i]) + "'"); | |
} | |
} | |
json = "{'__metadata':{'type':'" + listtype + "'}," | |
+ builder.toString() + "}"; | |
return json; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment