| 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 |
|
| 23568 |
govind |
15 |
import org.apache.logging.log4j.Logger;
|
|
|
16 |
import org.apache.logging.log4j.LogManager;
|
| 21570 |
ashik.ali |
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 |
|
| 23568 |
govind |
26 |
private static final Logger LOGGER = LogManager.getLogger(StringUtils.class);
|
| 23188 |
ashik.ali |
27 |
private static final String DATE_PATTERN = "dd/MM/yyyy";
|
| 23602 |
amit.gupta |
28 |
private static final String GADGET_COP_DATE_PATTERN = "MM/dd/yyyy";
|
| 23201 |
ashik.ali |
29 |
private static final String DATE_TIME_PATTERN = "dd/MM/yyyy HH:mm:ss";
|
| 24440 |
amit.gupta |
30 |
private static final String DATE_PATTERN_HYPHENATED = "dd-MM-yyyy";
|
| 21543 |
ashik.ali |
31 |
private StringUtils(){
|
|
|
32 |
|
|
|
33 |
}
|
| 22242 |
ashik.ali |
34 |
public static final LocalDate toDate(String dateString)throws DateTimeParseException{
|
| 22215 |
ashik.ali |
35 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN);
|
| 23188 |
ashik.ali |
36 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
|
|
37 |
return LocalDate.parse(dateString, dateTimeFormatter);
|
| 21543 |
ashik.ali |
38 |
}
|
|
|
39 |
|
| 23602 |
amit.gupta |
40 |
public static final String toGadgetCopDateString(LocalDate ldt) {
|
|
|
41 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(GADGET_COP_DATE_PATTERN);
|
|
|
42 |
return dateTimeFormatter.format(ldt);
|
|
|
43 |
}
|
|
|
44 |
|
| 22895 |
amit.gupta |
45 |
public static final LocalDate fromHypendatedDate(String dateString)throws DateTimeParseException{
|
|
|
46 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN_HYPHENATED);
|
| 23188 |
ashik.ali |
47 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
|
|
|
48 |
return LocalDate.parse(dateString, dateTimeFormatter);
|
| 22895 |
amit.gupta |
49 |
}
|
|
|
50 |
|
| 21543 |
ashik.ali |
51 |
public static final LocalTime toTime(String timeString) throws DateTimeParseException{
|
|
|
52 |
return LocalTime.parse(timeString);
|
|
|
53 |
}
|
| 21652 |
ashik.ali |
54 |
|
| 21792 |
ashik.ali |
55 |
public static final LocalDateTime toDateTime(long epocTime){
|
|
|
56 |
return Instant.ofEpochMilli(epocTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
57 |
}
|
|
|
58 |
|
| 22215 |
ashik.ali |
59 |
public static final String toString(LocalDate localDate){
|
|
|
60 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
| 22858 |
ashik.ali |
61 |
String formattedDateTime = localDate.format(formatter);
|
| 22215 |
ashik.ali |
62 |
return formattedDateTime;
|
|
|
63 |
}
|
| 24219 |
amit.gupta |
64 |
|
|
|
65 |
public static final String toHyphenatedString(LocalDate localDate){
|
|
|
66 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
|
|
|
67 |
String formattedDateTime = localDate.format(formatter);
|
|
|
68 |
return formattedDateTime;
|
|
|
69 |
}
|
| 22215 |
ashik.ali |
70 |
|
| 23017 |
ashik.ali |
71 |
public static final String toString(LocalDateTime localDateTime){
|
|
|
72 |
if(localDateTime == null){
|
|
|
73 |
return null;
|
|
|
74 |
}
|
|
|
75 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm:ss");
|
|
|
76 |
return localDateTime.format(formatter);
|
|
|
77 |
}
|
|
|
78 |
|
| 21652 |
ashik.ali |
79 |
public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
|
| 22290 |
ashik.ali |
80 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
| 21655 |
ashik.ali |
81 |
return null;
|
|
|
82 |
}
|
| 23201 |
ashik.ali |
83 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
|
|
|
84 |
return LocalDateTime.parse(dateTimeString, dateTimeFormatter);
|
| 21652 |
ashik.ali |
85 |
}
|
| 22858 |
ashik.ali |
86 |
|
|
|
87 |
public static final LocalDateTime toDateTime(String dateTimeString, DateTimePattern dateTimePattern) throws DateTimeParseException{
|
|
|
88 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
|
|
89 |
return null;
|
|
|
90 |
}
|
|
|
91 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern.getValue());
|
|
|
92 |
return LocalDateTime.parse(dateTimeString, formatter);
|
|
|
93 |
}
|
| 21543 |
ashik.ali |
94 |
|
|
|
95 |
public static boolean isValidMobile(String mobile){
|
|
|
96 |
try{
|
|
|
97 |
Long.valueOf(mobile);
|
|
|
98 |
}
|
|
|
99 |
catch(Exception e){
|
|
|
100 |
return false;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
if (mobile.startsWith("0")){
|
|
|
104 |
return false;
|
|
|
105 |
}
|
|
|
106 |
if (mobile.length()!=10){
|
|
|
107 |
return false;
|
|
|
108 |
}
|
|
|
109 |
return true;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public static boolean isValidEmailAddress(String email) {
|
|
|
113 |
boolean result = true;
|
|
|
114 |
try {
|
|
|
115 |
InternetAddress emailAddr = new InternetAddress(email);
|
|
|
116 |
emailAddr.validate();
|
|
|
117 |
} catch (Exception ex) {
|
|
|
118 |
result = false;
|
|
|
119 |
}
|
|
|
120 |
return result;
|
|
|
121 |
}
|
| 21570 |
ashik.ali |
122 |
|
| 23369 |
ashik.ali |
123 |
public static boolean isValidGstNumber(String gstNumber) {
|
| 23370 |
ashik.ali |
124 |
if(gstNumber == null || gstNumber.isEmpty() || gstNumber.length() == 15) {
|
| 23369 |
ashik.ali |
125 |
return true;
|
|
|
126 |
}
|
|
|
127 |
return false;
|
|
|
128 |
}
|
|
|
129 |
|
| 21570 |
ashik.ali |
130 |
public static List<String> getDuplicateElements(List<String> elements){
|
|
|
131 |
List<String> duplicates = new ArrayList<>();
|
|
|
132 |
for(int i = 0; i < elements.size(); i++){
|
|
|
133 |
for(int j = i + 1; j < elements.size(); j++){
|
|
|
134 |
if(elements.get(i).equals(elements.get(j))){
|
|
|
135 |
duplicates.add(elements.get(i));
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}
|
|
|
139 |
return duplicates;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public static String toString(Object object) throws Exception{
|
|
|
143 |
try {
|
|
|
144 |
return objectMapper.writeValueAsString(object);
|
|
|
145 |
} catch (JsonProcessingException e) {
|
|
|
146 |
LOGGER.error("Error occured while converting object to json", e);
|
|
|
147 |
throw e;
|
|
|
148 |
}
|
|
|
149 |
}
|
| 21756 |
ashik.ali |
150 |
public static boolean isValidPinCode(String pinCode){
|
|
|
151 |
if(pinCode == null || pinCode.isEmpty()){
|
|
|
152 |
return false;
|
|
|
153 |
}
|
|
|
154 |
if(pinCode.length() != 6){
|
|
|
155 |
return false;
|
|
|
156 |
}
|
|
|
157 |
if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
|
|
|
158 |
return false;
|
|
|
159 |
}
|
|
|
160 |
return true;
|
|
|
161 |
|
|
|
162 |
}
|
| 21792 |
ashik.ali |
163 |
|
| 22215 |
ashik.ali |
164 |
public static String generatePolicyNumber(String prefix, int sequence){
|
|
|
165 |
String policyNumber = String.format(prefix + "%06d", sequence);
|
| 23017 |
ashik.ali |
166 |
LOGGER.info("Generated Policy Number [{}]", policyNumber);
|
| 22215 |
ashik.ali |
167 |
return policyNumber;
|
|
|
168 |
}
|
| 22470 |
ashik.ali |
169 |
|
|
|
170 |
public static String generateFofoStoreSequence(String prefix, int sequence){
|
|
|
171 |
String fofoStoreSequenceNumber = String.format(prefix + "%03d", sequence);
|
| 23017 |
ashik.ali |
172 |
LOGGER.info("Generated Fofo Store Sequence Number [{}]", fofoStoreSequenceNumber);
|
| 22470 |
ashik.ali |
173 |
return fofoStoreSequenceNumber;
|
|
|
174 |
}
|
| 23017 |
ashik.ali |
175 |
|
| 23502 |
ashik.ali |
176 |
public static String generateRechageRequestId(String prefix, int sequence){
|
|
|
177 |
String oxigenRechargeRequestId = String.format(prefix + "%06d", sequence);
|
|
|
178 |
LOGGER.info("Generated Recharge Request Id [{}]", oxigenRechargeRequestId);
|
| 23017 |
ashik.ali |
179 |
return oxigenRechargeRequestId;
|
|
|
180 |
}
|
|
|
181 |
|
| 23780 |
ashik.ali |
182 |
public static String generateDeliveryNoteId(String prefix, int sequence){
|
|
|
183 |
String deliveryNoteId = String.format(prefix + "%06d", sequence);
|
|
|
184 |
LOGGER.info("Generated Delivery Note Id [{}]", deliveryNoteId);
|
|
|
185 |
return deliveryNoteId;
|
|
|
186 |
}
|
|
|
187 |
|
| 23017 |
ashik.ali |
188 |
public static String toOxigenRechargeRequestDate(LocalDateTime now){
|
|
|
189 |
StringBuilder oxigenRechargeRequestDate = new StringBuilder();
|
|
|
190 |
oxigenRechargeRequestDate.append(now.getYear());
|
|
|
191 |
oxigenRechargeRequestDate.append(now.getMonthValue());
|
|
|
192 |
oxigenRechargeRequestDate.append(now.getDayOfMonth());
|
|
|
193 |
oxigenRechargeRequestDate.append(now.getHour());
|
|
|
194 |
oxigenRechargeRequestDate.append(now.getMinute());
|
|
|
195 |
oxigenRechargeRequestDate.append(now.getSecond());
|
|
|
196 |
return oxigenRechargeRequestDate.toString();
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
public static LocalDateTime fromOxigenRechargeRequestDate(String oxigenRechargeRequestDate){
|
|
|
200 |
int year = Integer.parseInt(oxigenRechargeRequestDate.substring(0, 3));
|
|
|
201 |
int month = Integer.parseInt(oxigenRechargeRequestDate.substring(4, 5));
|
|
|
202 |
int dayOfMonth = Integer.parseInt(oxigenRechargeRequestDate.substring(6, 7));
|
|
|
203 |
int hour = Integer.parseInt(oxigenRechargeRequestDate.substring(8, 9));
|
|
|
204 |
int minute = Integer.parseInt(oxigenRechargeRequestDate.substring(10, 11));
|
|
|
205 |
int second = Integer.parseInt(oxigenRechargeRequestDate.substring(12, 13));
|
|
|
206 |
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
|
|
|
207 |
}
|
| 21675 |
ashik.ali |
208 |
|
| 21543 |
ashik.ali |
209 |
}
|