| 21543 |
ashik.ali |
1 |
package com.spice.profitmandi.common.util;
|
|
|
2 |
|
| 34813 |
aman |
3 |
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
4 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
5 |
import com.spice.profitmandi.common.enumuration.DateTimePattern;
|
|
|
6 |
import org.apache.logging.log4j.LogManager;
|
|
|
7 |
import org.apache.logging.log4j.Logger;
|
|
|
8 |
|
|
|
9 |
import javax.mail.internet.InternetAddress;
|
| 34859 |
vikas |
10 |
import java.text.Normalizer;
|
| 34723 |
vikas.jang |
11 |
import java.time.*;
|
| 22215 |
ashik.ali |
12 |
import java.time.format.DateTimeFormatter;
|
| 21543 |
ashik.ali |
13 |
import java.time.format.DateTimeParseException;
|
| 21570 |
ashik.ali |
14 |
import java.util.ArrayList;
|
|
|
15 |
import java.util.List;
|
| 34859 |
vikas |
16 |
import java.util.Locale;
|
| 34813 |
aman |
17 |
import java.util.regex.Matcher;
|
|
|
18 |
import java.util.regex.Pattern;
|
| 21543 |
ashik.ali |
19 |
|
|
|
20 |
public class StringUtils {
|
| 21570 |
ashik.ali |
21 |
|
|
|
22 |
private static ObjectMapper objectMapper = new ObjectMapper();
|
|
|
23 |
|
| 23568 |
govind |
24 |
private static final Logger LOGGER = LogManager.getLogger(StringUtils.class);
|
| 23188 |
ashik.ali |
25 |
private static final String DATE_PATTERN = "dd/MM/yyyy";
|
| 23602 |
amit.gupta |
26 |
private static final String GADGET_COP_DATE_PATTERN = "MM/dd/yyyy";
|
| 23201 |
ashik.ali |
27 |
private static final String DATE_TIME_PATTERN = "dd/MM/yyyy HH:mm:ss";
|
| 24440 |
amit.gupta |
28 |
private static final String DATE_PATTERN_HYPHENATED = "dd-MM-yyyy";
|
| 34492 |
vikas.jang |
29 |
private static final String DATE_TIME_ABBR = "dd/MM/yyyy hh:mma";
|
| 34813 |
aman |
30 |
private static final DateTimeFormatter SHORT_MONTH_DATE_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy");
|
|
|
31 |
private static final DateTimeFormatter SHORT_MONTH_DATE_YEAR_FORMATTER = DateTimeFormatter.ofPattern("dd-MMM-yy");
|
| 21543 |
ashik.ali |
32 |
private StringUtils(){
|
|
|
33 |
|
|
|
34 |
}
|
| 34813 |
aman |
35 |
|
| 37070 |
amit |
36 |
private static final Pattern WHITESPACE_RUN = Pattern.compile("[\\s\\u00A0\\u1680\\u2000-\\u200B\\u202F\\u205F\\u3000\\uFEFF]+");
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Normalizes whitespace: replaces every run of whitespace-like characters
|
|
|
40 |
* (spaces, tabs, CR/LF, non-breaking space , other Unicode spaces and
|
|
|
41 |
* zero-width characters) with a single regular space, then trims. Non-whitespace
|
|
|
42 |
* symbols (e.g. the degree sign) are preserved. Null-safe.
|
|
|
43 |
*/
|
|
|
44 |
public static String normalizeWhitespace(String value) {
|
|
|
45 |
if (value == null) {
|
|
|
46 |
return null;
|
|
|
47 |
}
|
|
|
48 |
return WHITESPACE_RUN.matcher(value).replaceAll(" ").trim();
|
|
|
49 |
}
|
|
|
50 |
|
| 34813 |
aman |
51 |
public static final LocalDate fromShortMonthDateYear(String dateString) throws DateTimeParseException {
|
|
|
52 |
dateString = dateString.toLowerCase();
|
|
|
53 |
dateString = StringUtils.capitalizeFirstChar(dateString);
|
|
|
54 |
return LocalDate.parse(dateString, SHORT_MONTH_DATE_YEAR_FORMATTER);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public static final LocalDate fromShortMonthDate(String dateString) throws DateTimeParseException {
|
|
|
58 |
dateString = StringUtils.capitalizeFirstChar(dateString);
|
|
|
59 |
return LocalDate.parse(dateString, SHORT_MONTH_DATE_FORMATTER);
|
|
|
60 |
}
|
|
|
61 |
|
| 34769 |
vikas.jang |
62 |
public static final LocalDate toDate(String dateString)throws DateTimeParseException{
|
| 22215 |
ashik.ali |
63 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN);
|
| 23188 |
ashik.ali |
64 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
|
|
65 |
return LocalDate.parse(dateString, dateTimeFormatter);
|
| 21543 |
ashik.ali |
66 |
}
|
|
|
67 |
|
| 34769 |
vikas.jang |
68 |
public static final LocalDate toDate(String dateString, String pattern)throws DateTimeParseException{
|
|
|
69 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, pattern);
|
|
|
70 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
|
|
71 |
return LocalDate.parse(dateString, dateTimeFormatter);
|
|
|
72 |
}
|
|
|
73 |
|
| 34813 |
aman |
74 |
public static final String toGadgetCopDateString(LocalDate ldt) {
|
| 23602 |
amit.gupta |
75 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(GADGET_COP_DATE_PATTERN);
|
|
|
76 |
return dateTimeFormatter.format(ldt);
|
|
|
77 |
}
|
|
|
78 |
|
| 22895 |
amit.gupta |
79 |
public static final LocalDate fromHypendatedDate(String dateString)throws DateTimeParseException{
|
|
|
80 |
LOGGER.info("Converting dateString [{}] with pattern[{}]", dateString, DATE_PATTERN_HYPHENATED);
|
| 23188 |
ashik.ali |
81 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
|
|
|
82 |
return LocalDate.parse(dateString, dateTimeFormatter);
|
| 22895 |
amit.gupta |
83 |
}
|
|
|
84 |
|
| 21543 |
ashik.ali |
85 |
public static final LocalTime toTime(String timeString) throws DateTimeParseException{
|
|
|
86 |
return LocalTime.parse(timeString);
|
|
|
87 |
}
|
| 34723 |
vikas.jang |
88 |
|
| 34761 |
vikas.jang |
89 |
public static final LocalTime secondsToTime(double totalSeconds) throws DateTimeParseException{
|
|
|
90 |
int totalSecs = (int) Math.round(totalSeconds);
|
|
|
91 |
int hours = totalSecs / 3600;
|
|
|
92 |
int minutes = (totalSecs % 3600) / 60;
|
|
|
93 |
int seconds = totalSecs % 60;
|
| 34723 |
vikas.jang |
94 |
String timeString = String.format("%02d:%02d:%02d", hours, minutes, seconds);
|
|
|
95 |
return LocalTime.parse(timeString);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
public static final LocalTime timeDifference(LocalTime startTime, LocalTime endTime) throws DateTimeParseException{
|
|
|
99 |
Duration duration = Duration.between(startTime, endTime);
|
|
|
100 |
if (duration.isNegative()) {
|
|
|
101 |
duration = duration.plusHours(24); // handle case when now is past midnight
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
long hours = duration.toHours();
|
|
|
105 |
long minutes = duration.toMinutes() % 60;
|
|
|
106 |
long seconds = duration.getSeconds() % 60;
|
|
|
107 |
|
|
|
108 |
return LocalTime.of((int) hours % 24, (int) minutes, (int) seconds);
|
|
|
109 |
}
|
| 21652 |
ashik.ali |
110 |
|
| 21792 |
ashik.ali |
111 |
public static final LocalDateTime toDateTime(long epocTime){
|
|
|
112 |
return Instant.ofEpochMilli(epocTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
113 |
}
|
|
|
114 |
|
| 22215 |
ashik.ali |
115 |
public static final String toString(LocalDate localDate){
|
|
|
116 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN);
|
| 22858 |
ashik.ali |
117 |
String formattedDateTime = localDate.format(formatter);
|
| 22215 |
ashik.ali |
118 |
return formattedDateTime;
|
|
|
119 |
}
|
| 24219 |
amit.gupta |
120 |
|
|
|
121 |
public static final String toHyphenatedString(LocalDate localDate){
|
|
|
122 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_PATTERN_HYPHENATED);
|
|
|
123 |
String formattedDateTime = localDate.format(formatter);
|
|
|
124 |
return formattedDateTime;
|
|
|
125 |
}
|
| 22215 |
ashik.ali |
126 |
|
| 23017 |
ashik.ali |
127 |
public static final String toString(LocalDateTime localDateTime){
|
|
|
128 |
if(localDateTime == null){
|
|
|
129 |
return null;
|
|
|
130 |
}
|
|
|
131 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY HH:mm:ss");
|
|
|
132 |
return localDateTime.format(formatter);
|
|
|
133 |
}
|
|
|
134 |
|
| 21652 |
ashik.ali |
135 |
public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
|
| 22290 |
ashik.ali |
136 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
| 21655 |
ashik.ali |
137 |
return null;
|
|
|
138 |
}
|
| 23201 |
ashik.ali |
139 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
|
|
|
140 |
return LocalDateTime.parse(dateTimeString, dateTimeFormatter);
|
| 21652 |
ashik.ali |
141 |
}
|
| 34492 |
vikas.jang |
142 |
|
|
|
143 |
public static final String toLocalDateTime(String dateTimeString) throws DateTimeParseException{
|
|
|
144 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
|
|
145 |
return null;
|
|
|
146 |
}
|
|
|
147 |
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
|
|
148 |
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(DATE_TIME_ABBR);
|
|
|
149 |
return dateTime.format(outputFormatter);
|
|
|
150 |
}
|
| 34813 |
aman |
151 |
|
| 22858 |
ashik.ali |
152 |
public static final LocalDateTime toDateTime(String dateTimeString, DateTimePattern dateTimePattern) throws DateTimeParseException{
|
|
|
153 |
if(dateTimeString == null || dateTimeString.equals("0") || dateTimeString.isEmpty()){
|
|
|
154 |
return null;
|
|
|
155 |
}
|
|
|
156 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimePattern.getValue());
|
|
|
157 |
return LocalDateTime.parse(dateTimeString, formatter);
|
|
|
158 |
}
|
| 21543 |
ashik.ali |
159 |
|
|
|
160 |
public static boolean isValidMobile(String mobile){
|
|
|
161 |
try{
|
|
|
162 |
Long.valueOf(mobile);
|
|
|
163 |
}
|
|
|
164 |
catch(Exception e){
|
|
|
165 |
return false;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if (mobile.startsWith("0")){
|
|
|
169 |
return false;
|
|
|
170 |
}
|
|
|
171 |
if (mobile.length()!=10){
|
|
|
172 |
return false;
|
|
|
173 |
}
|
|
|
174 |
return true;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
public static boolean isValidEmailAddress(String email) {
|
|
|
178 |
boolean result = true;
|
|
|
179 |
try {
|
|
|
180 |
InternetAddress emailAddr = new InternetAddress(email);
|
|
|
181 |
emailAddr.validate();
|
|
|
182 |
} catch (Exception ex) {
|
|
|
183 |
result = false;
|
|
|
184 |
}
|
|
|
185 |
return result;
|
|
|
186 |
}
|
| 21570 |
ashik.ali |
187 |
|
| 23369 |
ashik.ali |
188 |
public static boolean isValidGstNumber(String gstNumber) {
|
| 23370 |
ashik.ali |
189 |
if(gstNumber == null || gstNumber.isEmpty() || gstNumber.length() == 15) {
|
| 23369 |
ashik.ali |
190 |
return true;
|
|
|
191 |
}
|
|
|
192 |
return false;
|
|
|
193 |
}
|
|
|
194 |
|
| 21570 |
ashik.ali |
195 |
public static List<String> getDuplicateElements(List<String> elements){
|
|
|
196 |
List<String> duplicates = new ArrayList<>();
|
|
|
197 |
for(int i = 0; i < elements.size(); i++){
|
|
|
198 |
for(int j = i + 1; j < elements.size(); j++){
|
|
|
199 |
if(elements.get(i).equals(elements.get(j))){
|
|
|
200 |
duplicates.add(elements.get(i));
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
}
|
|
|
204 |
return duplicates;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
public static String toString(Object object) throws Exception{
|
|
|
208 |
try {
|
|
|
209 |
return objectMapper.writeValueAsString(object);
|
|
|
210 |
} catch (JsonProcessingException e) {
|
|
|
211 |
LOGGER.error("Error occured while converting object to json", e);
|
|
|
212 |
throw e;
|
|
|
213 |
}
|
|
|
214 |
}
|
| 21756 |
ashik.ali |
215 |
public static boolean isValidPinCode(String pinCode){
|
|
|
216 |
if(pinCode == null || pinCode.isEmpty()){
|
|
|
217 |
return false;
|
|
|
218 |
}
|
|
|
219 |
if(pinCode.length() != 6){
|
|
|
220 |
return false;
|
|
|
221 |
}
|
|
|
222 |
if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
|
|
|
223 |
return false;
|
|
|
224 |
}
|
|
|
225 |
return true;
|
|
|
226 |
|
|
|
227 |
}
|
| 21792 |
ashik.ali |
228 |
|
| 22215 |
ashik.ali |
229 |
public static String generatePolicyNumber(String prefix, int sequence){
|
|
|
230 |
String policyNumber = String.format(prefix + "%06d", sequence);
|
| 23017 |
ashik.ali |
231 |
LOGGER.info("Generated Policy Number [{}]", policyNumber);
|
| 22215 |
ashik.ali |
232 |
return policyNumber;
|
|
|
233 |
}
|
| 22470 |
ashik.ali |
234 |
|
|
|
235 |
public static String generateFofoStoreSequence(String prefix, int sequence){
|
|
|
236 |
String fofoStoreSequenceNumber = String.format(prefix + "%03d", sequence);
|
| 23017 |
ashik.ali |
237 |
LOGGER.info("Generated Fofo Store Sequence Number [{}]", fofoStoreSequenceNumber);
|
| 22470 |
ashik.ali |
238 |
return fofoStoreSequenceNumber;
|
|
|
239 |
}
|
| 23017 |
ashik.ali |
240 |
|
| 23502 |
ashik.ali |
241 |
public static String generateRechageRequestId(String prefix, int sequence){
|
|
|
242 |
String oxigenRechargeRequestId = String.format(prefix + "%06d", sequence);
|
|
|
243 |
LOGGER.info("Generated Recharge Request Id [{}]", oxigenRechargeRequestId);
|
| 23017 |
ashik.ali |
244 |
return oxigenRechargeRequestId;
|
|
|
245 |
}
|
|
|
246 |
|
| 23780 |
ashik.ali |
247 |
public static String generateDeliveryNoteId(String prefix, int sequence){
|
|
|
248 |
String deliveryNoteId = String.format(prefix + "%06d", sequence);
|
|
|
249 |
LOGGER.info("Generated Delivery Note Id [{}]", deliveryNoteId);
|
|
|
250 |
return deliveryNoteId;
|
|
|
251 |
}
|
|
|
252 |
|
| 23017 |
ashik.ali |
253 |
public static String toOxigenRechargeRequestDate(LocalDateTime now){
|
|
|
254 |
StringBuilder oxigenRechargeRequestDate = new StringBuilder();
|
|
|
255 |
oxigenRechargeRequestDate.append(now.getYear());
|
|
|
256 |
oxigenRechargeRequestDate.append(now.getMonthValue());
|
|
|
257 |
oxigenRechargeRequestDate.append(now.getDayOfMonth());
|
|
|
258 |
oxigenRechargeRequestDate.append(now.getHour());
|
|
|
259 |
oxigenRechargeRequestDate.append(now.getMinute());
|
|
|
260 |
oxigenRechargeRequestDate.append(now.getSecond());
|
|
|
261 |
return oxigenRechargeRequestDate.toString();
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
public static LocalDateTime fromOxigenRechargeRequestDate(String oxigenRechargeRequestDate){
|
|
|
265 |
int year = Integer.parseInt(oxigenRechargeRequestDate.substring(0, 3));
|
|
|
266 |
int month = Integer.parseInt(oxigenRechargeRequestDate.substring(4, 5));
|
|
|
267 |
int dayOfMonth = Integer.parseInt(oxigenRechargeRequestDate.substring(6, 7));
|
|
|
268 |
int hour = Integer.parseInt(oxigenRechargeRequestDate.substring(8, 9));
|
|
|
269 |
int minute = Integer.parseInt(oxigenRechargeRequestDate.substring(10, 11));
|
|
|
270 |
int second = Integer.parseInt(oxigenRechargeRequestDate.substring(12, 13));
|
|
|
271 |
return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
|
|
|
272 |
}
|
| 21675 |
ashik.ali |
273 |
|
| 34813 |
aman |
274 |
public static String capitalizeFirstChar(String input) {
|
|
|
275 |
if (input == null || input.isEmpty()) {
|
|
|
276 |
return input; // Return the input if it's null or empty
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
// Define the regular expression to match the first alphabetic character
|
|
|
280 |
Pattern pattern = Pattern.compile("([a-zA-Z])");
|
|
|
281 |
Matcher matcher = pattern.matcher(input);
|
|
|
282 |
|
|
|
283 |
// If the first character matches, capitalize it
|
|
|
284 |
if (matcher.find()) {
|
|
|
285 |
return matcher.replaceFirst(matcher.group(1).toUpperCase());
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
return input; // If no match is found, return the original string
|
|
|
289 |
}
|
|
|
290 |
|
| 34761 |
vikas.jang |
291 |
public static String formatDistance(double distanceInMeters) {
|
| 34835 |
vikas |
292 |
if (distanceInMeters <= 950) {
|
|
|
293 |
int roundedMeters = ((int) Math.ceil(distanceInMeters / 100.0)) * 100;
|
|
|
294 |
double distanceKm = roundedMeters / 1000.0;
|
|
|
295 |
return String.format("%.2f", distanceKm);
|
| 34761 |
vikas.jang |
296 |
} else {
|
| 34835 |
vikas |
297 |
return String.format("%.2f", distanceInMeters / 1000.0);
|
| 34761 |
vikas.jang |
298 |
}
|
|
|
299 |
}
|
|
|
300 |
|
| 34859 |
vikas |
301 |
public static String toSlug(String input) {
|
|
|
302 |
if (input == null) return "";
|
|
|
303 |
String normalized = Normalizer.normalize(input, Normalizer.Form.NFD).replaceAll("\\p{M}", "");
|
|
|
304 |
String lowerCase = normalized.toLowerCase(Locale.ENGLISH);
|
|
|
305 |
String slug = lowerCase.replaceAll("[^a-z0-9]+", "-");
|
|
|
306 |
slug = slug.replaceAll("^-+|-+$", "");
|
|
|
307 |
return slug;
|
|
|
308 |
}
|
|
|
309 |
|
| 21543 |
ashik.ali |
310 |
}
|