Done ticket GEPAFINBE-3
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
public class DateTimeUtil {
|
||||
|
||||
|
||||
public static LocalDateTime DateServerToUTC(LocalDateTime systemDate) {
|
||||
|
||||
ZonedDateTime ldtZoned = systemDate.atZone(ZoneId.systemDefault());
|
||||
LocalDateTime localDatetime = ldtZoned.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
|
||||
return localDatetime;
|
||||
}
|
||||
|
||||
public static LocalDateTime getPreviousMonthDate(int month) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
c.add(Calendar.MONTH, -month);
|
||||
LocalDateTime conv = LocalDateTime.ofInstant(c.getTime().toInstant(), ZoneId.systemDefault());
|
||||
return conv;
|
||||
}
|
||||
|
||||
public static Date getDateWithoutTime(LocalDateTime systemDate) {
|
||||
ZonedDateTime zdt = systemDate.atZone(ZoneId.systemDefault());
|
||||
Date date = Date.from(zdt.toInstant());
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||
calendar.set(Calendar.MINUTE, 0);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertDateToLocalDateTime(Date date) {
|
||||
LocalDateTime ldt = LocalDateTime.ofInstant(date.toInstant(),
|
||||
ZoneId.systemDefault());
|
||||
return ldt;
|
||||
}
|
||||
|
||||
|
||||
public static Date convertLocalDateTimeToDateUsingInstant(LocalDateTime localDateTime) {
|
||||
return Date
|
||||
.from(localDateTime.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class ObjectUtils {
|
||||
|
||||
public static <T> void setIfNotNull(Consumer<T> setter, T value) {
|
||||
if (value != null) {
|
||||
setter.accept(value);
|
||||
}
|
||||
}
|
||||
public static <T> void setIfUpdated(Supplier<T> getter, Consumer<T> setter, T newValue) {
|
||||
T currentValue = getter.get();
|
||||
if (newValue != null && !newValue.equals(currentValue)) {
|
||||
setter.accept(newValue);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
49
src/main/java/net/gepafin/tendermanagement/util/Utils.java
Normal file
49
src/main/java/net/gepafin/tendermanagement/util/Utils.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static final Logger log = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper()
|
||||
.registerModule(new JavaTimeModule())
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
public static <T, U> U convertObject(T source, Class<U> destinationClass) {
|
||||
try {
|
||||
return mapper.convertValue(source, destinationClass);
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting object: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, U> List<U> convertSourceListToDestinationList(List<T> sourceList, Class<U> destinationClass) {
|
||||
try {
|
||||
return sourceList.stream()
|
||||
.map(source -> mapper.convertValue(source, destinationClass))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting list: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, U> List<U> convertSourceToList(T source, Class<U> destinationClass) {
|
||||
try {
|
||||
// Convert single source object to a single-element list of destination type
|
||||
return List.of(mapper.convertValue(source, destinationClass));
|
||||
} catch (Exception e) {
|
||||
log.error("Error converting single object to list: " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user