
org.ikasan.dashboard.ui.util.DateFormatter Maven / Gradle / Ivy
The 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 String DATE_FORMAT_LONG = "dd MMMM yyyy HH:mm:ss.SSS '['VV '-' z']'";
public static final DateTimeFormatter DATE_FORMAT_WITH_TIMEZONE = DateTimeFormatter.ISO_ZONED_DATE_TIME;
private DateTimeFormatter tableFormatter;
private DateTimeFormatter longFormatter;
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() {
this.tableFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_TABLE_VIEWS);
this.longFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT_LONG);
}
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);
}
public String getLongFormattedDate(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.longFormatter.format(zdt);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy