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