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