Subversion Repositories SmartDukaan

Rev

Rev 21756 | Rev 21986 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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;
21543 ashik.ali 8
import java.time.format.DateTimeParseException;
21570 ashik.ali 9
import java.util.ArrayList;
10
import java.util.List;
21543 ashik.ali 11
 
12
import javax.mail.internet.InternetAddress;
13
 
21570 ashik.ali 14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
 
17
import com.fasterxml.jackson.core.JsonProcessingException;
18
import com.fasterxml.jackson.databind.ObjectMapper;
19
 
21543 ashik.ali 20
public class StringUtils {
21570 ashik.ali 21
 
22
	private static ObjectMapper objectMapper = new ObjectMapper();
23
 
24
	private static final Logger LOGGER = LoggerFactory.getLogger(StringUtils.class);
25
 
21543 ashik.ali 26
	private StringUtils(){
27
 
28
	}
29
	public static final LocalDate toDate(String dateString)throws DateTimeParseException{
30
		return LocalDate.parse(dateString);
31
	}
32
 
33
	public static final LocalTime toTime(String timeString) throws DateTimeParseException{
34
		return LocalTime.parse(timeString);
35
	}
21652 ashik.ali 36
 
21792 ashik.ali 37
	public static final LocalDateTime toDateTime(long epocTime){
38
		return Instant.ofEpochMilli(epocTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
39
	}
40
 
21652 ashik.ali 41
	public static final LocalDateTime toDateTime(String dateTimeString) throws DateTimeParseException{
21655 ashik.ali 42
		if(dateTimeString == null){
43
			return null;
44
		}
21652 ashik.ali 45
		return LocalDateTime.parse(dateTimeString);
46
	}
21543 ashik.ali 47
 
48
	public static boolean isValidMobile(String mobile){
49
		try{
50
			Long.valueOf(mobile);
51
		}
52
		catch(Exception e){
53
			return false;
54
		}
55
 
56
		if (mobile.startsWith("0")){
57
			return false;
58
		}
59
		if (mobile.length()!=10){
60
			return false;
61
		}
62
		return true;
63
	}
64
 
65
	public static boolean isValidEmailAddress(String email) {
66
		boolean result = true;
67
		try {
68
			InternetAddress emailAddr = new InternetAddress(email);
69
			emailAddr.validate();
70
		} catch (Exception ex) {
71
			result = false;
72
		}
73
		return result;
74
	}
21570 ashik.ali 75
 
76
	public static List<String> getDuplicateElements(List<String> elements){
77
		List<String> duplicates = new ArrayList<>();
78
		for(int i = 0; i < elements.size(); i++){
79
			for(int j = i + 1; j < elements.size(); j++){
80
				if(elements.get(i).equals(elements.get(j))){
81
					duplicates.add(elements.get(i));
82
				}
83
			}
84
		}
85
		return duplicates;
86
	}
87
 
88
	public static String toString(Object object) throws Exception{
89
		try {
90
			return objectMapper.writeValueAsString(object);
91
		} catch (JsonProcessingException e) {
92
			LOGGER.error("Error occured while converting object to json", e);
93
			throw e;
94
		}
95
	}
21756 ashik.ali 96
	public static boolean isValidPinCode(String pinCode){
97
		if(pinCode == null || pinCode.isEmpty()){
98
			return false;
99
		}
100
		if(pinCode.length() != 6){
101
			return false;
102
		}
103
		if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
104
			return false;
105
		}
106
		return true;
107
 
108
	}
21792 ashik.ali 109
 
110
 
21675 ashik.ali 111
 
21543 ashik.ali 112
}