| 21543 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
| 21792 |
ashik.ali |
3 |
import java.time.Instant;
|
| 21543 |
ashik.ali |
4 |
import java.time.LocalDate;
|
| 21652 |
ashik.ali |
5 |
import java.time.LocalDateTime;
|
| 21543 |
ashik.ali |
6 |
import java.time.LocalTime;
|
| 21792 |
ashik.ali |
7 |
import java.time.ZoneId;
|
| 22215 |
ashik.ali |
8 |
import java.time.format.DateTimeFormatter;
|
| 21543 |
ashik.ali |
9 |
import java.time.format.DateTimeParseException;
|
| 21570 |
ashik.ali |
10 |
import java.util.ArrayList;
|
|
|
11 |
import java.util.List;
|
| 21543 |
ashik.ali |
12 |
|
|
|
13 |
import javax.mail.internet.InternetAddress;
|
|
|
14 |
|
| 21570 |
ashik.ali |
15 |
import org.slf4j.Logger;
|
|
|
16 |
import org.slf4j.LoggerFactory;
|
|
|
17 |
|
|
|
18 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
19 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 22858 |
ashik.ali |
20 |
import com.spice.profitmandi.common.enumuration.DateTimePattern;
|
| 21570 |
ashik.ali |
21 |
|
| 21543 |
ashik.ali |
22 |
public class StringUtils {
|
| 21570 |
ashik.ali |
23 |
|
|
|
24 |
private static ObjectMapper objectMapper = new ObjectMapper();
|
|
|
25 |
|
|
|
26 |
private static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class);
|
| 22215 |
ashik.ali |
27 |
private static final String DATE_PATTERN = "MM/dd/yyyy";
|
| 22895 |
amit.gupta |
28 |
private static final String DATE_PATTERN_HYPHENATED = "yyyy-MM-dd";
|
| 21543 |
ashik.ali |
29 |
private StringUtils(){
|
|
|
30 |
|
|
|
31 |
}
|
| 22242 |
ashik.ali |
32 |
public static final LocalDate toDate(String dateString)throws DateTimeParseException{
|
| 22215 |
ashik.ali |
33 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN);
|
| 22242 |
ashik.ali |
34 |
return LocalDate.parse(dateString);
|
| 21543 |
ashik.ali |
35 |
}
|
|
|
36 |
|
| 22895 |
amit.gupta |
37 |
public static final LocalDate fromHypendatedDate(String dateString)throws DateTimeParseException{
|
|
|
38 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN_HYPHENATED);
|
|
|
39 |
return LocalDate.parse(dateString);
|
|
|
40 |
}
|
|
|
41 |
|
| 21543 |
ashik.ali |
42 |
public static final LocalTime toTime(String timeString) throws DateTimeParseException{
|
|
|
43 |
return LocalTime.parse(timeString);
|
|
|
44 |
}
|
| 21652 |
ashik.ali |
45 |
|
| 21792 |
ashik.ali |
46 |
public static final LocalDateTime toDateTime(long epocTime){
|
|
|
47 |
return Instant.ofEpochMilli(epocTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
48 |
}
|
|
|
49 |
|
| 22215 |
ashik.ali |
50 |
public static final String toString(LocalDate localDate){
|
|
|
51 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
| 22858 |
ashik.ali |
52 |
String formattedDateTime = localDate.format(formatter);
|
| 22215 |
ashik.ali |
53 |
return formattedDateTime;
|
|
|
54 |
}
|
|
|
55 |
|
| 21652 |
ashik.ali |
56 |
public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
|
| 22290 |
ashik.ali |
57 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
| 21655 |
ashik.ali |
58 |
return null;
|
|
|
59 |
}
|
| 22858 |
ashik.ali |
60 |
return LocalDateTime.parse(dateTimeString);
|
| 21652 |
ashik.ali |
61 |
}
|
| 22858 |
ashik.ali |
62 |
|
|
|
63 |
public static final LocalDateTime toDateTime(String dateTimeString, DateTimePattern dateTimePattern) throws DateTimeParseException{
|
|
|
64 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
|
|
65 |
return null;
|
|
|
66 |
}
|
|
|
67 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern.getValue());
|
|
|
68 |
return LocalDateTime.parse(dateTimeString, formatter);
|
|
|
69 |
}
|
| 21543 |
ashik.ali |
70 |
|
|
|
71 |
public static boolean isValidMobile(String mobile){
|
|
|
72 |
try{
|
|
|
73 |
Long.valueOf(mobile);
|
|
|
74 |
}
|
|
|
75 |
catch(Exception e){
|
|
|
76 |
return false;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if (mobile.startsWith("0")){
|
|
|
80 |
return false;
|
|
|
81 |
}
|
|
|
82 |
if (mobile.length()!=10){
|
|
|
83 |
return false;
|
|
|
84 |
}
|
|
|
85 |
return true;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public static boolean isValidEmailAddress(String email) {
|
|
|
89 |
boolean result = true;
|
|
|
90 |
try {
|
|
|
91 |
InternetAddress emailAddr = new InternetAddress(email);
|
|
|
92 |
emailAddr.validate();
|
|
|
93 |
} catch (Exception ex) {
|
|
|
94 |
result = false;
|
|
|
95 |
}
|
|
|
96 |
return result;
|
|
|
97 |
}
|
| 21570 |
ashik.ali |
98 |
|
|
|
99 |
public static List<String> getDuplicateElements(List<String> elements){
|
|
|
100 |
List<String> duplicates = new ArrayList<>();
|
|
|
101 |
for(int i = 0; i < elements.size(); i++){
|
|
|
102 |
for(int j = i + 1; j < elements.size(); j++){
|
|
|
103 |
if(elements.get(i).equals(elements.get(j))){
|
|
|
104 |
duplicates.add(elements.get(i));
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
return duplicates;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public static String toString(Object object) throws Exception{
|
|
|
112 |
try {
|
|
|
113 |
return objectMapper.writeValueAsString(object);
|
|
|
114 |
} catch (JsonProcessingException e) {
|
|
|
115 |
LOGGER.error("Error occured while converting object to json", e);
|
|
|
116 |
throw e;
|
|
|
117 |
}
|
|
|
118 |
}
|
| 21756 |
ashik.ali |
119 |
public static boolean isValidPinCode(String pinCode){
|
|
|
120 |
if(pinCode == null || pinCode.isEmpty()){
|
|
|
121 |
return false;
|
|
|
122 |
}
|
|
|
123 |
if(pinCode.length() != 6){
|
|
|
124 |
return false;
|
|
|
125 |
}
|
|
|
126 |
if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
|
|
|
127 |
return false;
|
|
|
128 |
}
|
|
|
129 |
return true;
|
|
|
130 |
|
|
|
131 |
}
|
| 21792 |
ashik.ali |
132 |
|
| 22215 |
ashik.ali |
133 |
public static String generatePolicyNumber(String prefix, int sequence){
|
|
|
134 |
String policyNumber = String.format(prefix + "%06d", sequence);
|
|
|
135 |
LOGGER.info("Generated Policy Number {}", policyNumber);
|
|
|
136 |
return policyNumber;
|
|
|
137 |
}
|
| 22470 |
ashik.ali |
138 |
|
|
|
139 |
public static String generateFofoStoreSequence(String prefix, int sequence){
|
|
|
140 |
String fofoStoreSequenceNumber = String.format(prefix + "%03d", sequence);
|
|
|
141 |
LOGGER.info("Generated Fofo Store Sequence Number {}", fofoStoreSequenceNumber);
|
|
|
142 |
return fofoStoreSequenceNumber;
|
|
|
143 |
}
|
| 21675 |
ashik.ali |
144 |
|
| 21543 |
ashik.ali |
145 |
}
|