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