All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ikasan.dashboard.ui.util.DateFormatter Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.ikasan.dashboard.ui.util;

import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class DateFormatter
{
    public static final String DATE_FORMAT_TABLE_VIEWS = "dd/MM/yyyy HH:mm:ss.SSS '['VV '-' z']'";
    public static final DateTimeFormatter DATE_FORMAT_WITH_TIMEZONE = DateTimeFormatter.ISO_ZONED_DATE_TIME;

    private DateTimeFormatter tableFormatter;
    private ZoneId zoneId;
    private static DateFormatter instance;

    public static DateFormatter instance() {
        if(instance == null) {
            instance = new DateFormatter();
        }

        return instance;
    }

    public static DateFormatter instance(ZoneId zoneId) {
        if(instance == null) {
            instance = new DateFormatter(zoneId);
        }

        return instance;
    }

    public DateFormatter() {
        tableFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_TABLE_VIEWS);
    }

    public DateFormatter(ZoneId zoneId) {
        this();
        this.zoneId = zoneId;
    }

    public String getFormattedDate(long timestamp)
    {
        if(timestamp == 0)
        {
            return "N/A";
        }

        ZonedDateTime zdt;

        if(this.zoneId != null) {
            zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp),
                this.zoneId);
        }
        else {
            zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp),
                DateTimeUtil.getZoneId());
        }

        return this.tableFormatter.format(zdt);
    }

    public String getFormattedDate(ZonedDateTime dateTime)
    {
        return DATE_FORMAT_WITH_TIMEZONE.format(dateTime);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy