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