Active MQ, Camel, Cassandra, Couchbase, IBM MQ, Java, Maven, Neo4j, Spring

Sample Date and Time Conversion Utility Class with most of the conversions possible.

Dear Readers, I have added an Utility class that can be leveraged to use in your project to convert date and times based on your need.

@JsonInclude(JsonInclude.Include.NON_NULL)
public class BlogDateTimeConversion {

    public static final DateTimeZone GRAPH_TIMEZONE = DateTimeZone.UTC;

    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter
            .ofPattern("yyyy-MM-dd HH:mm:ss");
    
    private static final String UTC_ZONE = "UTC";
    
    private static final String NULL_PROPERTY_VALUE = "null";
    
    private static final String COMMA_SEPARATOR = ",";
    
    
    @JsonSerialize(using = DateTimeSerializer.class)
    @JsonDeserialize(using = DateTimeDeserializer.class)
    @ApiModelProperty(hidden = true)
    private DateTime modifiedDate;

    public static LocalDateTime convertEpochMilliToLocalDateTime(Long epochTime) {
        if (epochTime == null) {
            return null;
        }

        return new LocalDateTime(epochTime, GRAPH_TIMEZONE);
    }

    public static DateTime convertLongToDateTime(Long millis) {
        if (millis == null
                || millis == 0) {
            return null;
        }
        return new DateTime(millis, GRAPH_TIMEZONE);
    }

    public static OffsetDateTime convertLongToOffsetDateTime(Long millis) {
        if (millis == null
                || millis == 0) {
            return null;
        }
        return OffsetDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault());
    }

    public static Long convertDateTimeToLong(DateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.withZone(GRAPH_TIMEZONE).getMillis();
    }

    public static Long convertLocalDateTimeToEpochMilli(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }

        return localDateTime.toDateTime(GRAPH_TIMEZONE).getMillis();
    }

    public static Long convertOffsetDateTimeToEpochMilli(OffsetDateTime offsetDateTime) {
        if (offsetDateTime == null) {
            return null;
        }

        return offsetDateTime.toLocalDateTime().toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    public static String convertLocalDateTimeToString(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return StringUtils.EMPTY;
        }

        return String.valueOf(convertLocalDateTimeToEpochMilli(localDateTime));
    }


    public static String easternTime(String timestamp) {
        String etTime = null;
        String nTimeStamp = StringUtils.strip(StringUtils.stripToNull(timestamp),
                COMMA_SEPARATOR);
        if (!nTimeStamp.equalsIgnoreCase(NULL_PROPERTY_VALUE)) {
            etTime = DATE_FORMATTER.format(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(Long.parseLong(nTimeStamp)), ZoneId.systemDefault()));
        }
        return new StringBuilder().append(COMMA_SEPARATOR).append(etTime).toString();
    }

    public static String easternTime(Long timestamp) {
        if (timestamp != null) {
            return DATE_FORMATTER.format(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
        }
        return null;
    }

    public static String utcTime(String timestamp) {
        String utcTime = null;
        String nTimeStamp = StringUtils.strip(StringUtils.stripToNull(timestamp),
                COMMA_SEPARATOR);
        if (!nTimeStamp.equalsIgnoreCase(NULL_PROPERTY_VALUE)) {
            utcTime = DATE_FORMATTER.format(LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(Long.parseLong(nTimeStamp)), ZoneId.of(UTC_ZONE)));
        }
        return new StringBuilder().append(COMMA_SEPARATOR).append(utcTime).toString();
    }

    public static String utcTime(Long timestamp) {
        if (timestamp != null) {
            return DATE_FORMATTER.format(
                    LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of(UTC_ZONE)));
        }
        return null;
    }

}

Leave a Reply