Subversion Repositories SmartDukaan

Rev

Rev 21792 | Rev 22215 | 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{
21986 kshitij.so 42
		if(dateTimeString == null || dateTimeString.equals("0")){
21655 ashik.ali 43
			return null;
44
		}
21986 kshitij.so 45
		LocalDateTime date =
46
				  Instant.ofEpochMilli(Long.valueOf(dateTimeString))
47
				  .atZone(ZoneId.systemDefault())
48
				  .toLocalDateTime();
49
		return date;
21652 ashik.ali 50
	}
21543 ashik.ali 51
 
52
	public static boolean isValidMobile(String mobile){
53
		try{
54
			Long.valueOf(mobile);
55
		}
56
		catch(Exception e){
57
			return false;
58
		}
59
 
60
		if (mobile.startsWith("0")){
61
			return false;
62
		}
63
		if (mobile.length()!=10){
64
			return false;
65
		}
66
		return true;
67
	}
68
 
69
	public static boolean isValidEmailAddress(String email) {
70
		boolean result = true;
71
		try {
72
			InternetAddress emailAddr = new InternetAddress(email);
73
			emailAddr.validate();
74
		} catch (Exception ex) {
75
			result = false;
76
		}
77
		return result;
78
	}
21570 ashik.ali 79
 
80
	public static List<String> getDuplicateElements(List<String> elements){
81
		List<String> duplicates = new ArrayList<>();
82
		for(int i = 0; i < elements.size(); i++){
83
			for(int j = i + 1; j < elements.size(); j++){
84
				if(elements.get(i).equals(elements.get(j))){
85
					duplicates.add(elements.get(i));
86
				}
87
			}
88
		}
89
		return duplicates;
90
	}
91
 
92
	public static String toString(Object object) throws Exception{
93
		try {
94
			return objectMapper.writeValueAsString(object);
95
		} catch (JsonProcessingException e) {
96
			LOGGER.error("Error occured while converting object to json", e);
97
			throw e;
98
		}
99
	}
21756 ashik.ali 100
	public static boolean isValidPinCode(String pinCode){
101
		if(pinCode == null || pinCode.isEmpty()){
102
			return false;
103
		}
104
		if(pinCode.length() != 6){
105
			return false;
106
		}
107
		if(pinCode.startsWith("00") || pinCode.startsWith("01") || pinCode.startsWith("10")){
108
			return false;
109
		}
110
		return true;
111
 
112
	}
21792 ashik.ali 113
 
114
 
21675 ashik.ali 115
 
21543 ashik.ali 116
}