
clime.messadmin.utils.DateUtils Maven / Gradle / Ivy
The newest version!
/**
*
*/
package clime.messadmin.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import clime.messadmin.i18n.I18NSupport;
/**
* @author Cédrik LIME
*/
public abstract class DateUtils {
/**
* Default ISO 8601 datetime format: {@value}
* @see ISO 8601 / European Standard EN 28601 DateTime
* @see RFC 3399
*/
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";//$NON-NLS-1$
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";//$NON-NLS-1$
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";//$NON-NLS-1$
/**
*
*/
private DateUtils() {
super();
}
public static String dateToFormattedDateTimeString(long value, String format) {
String lc_format = format;
if (null == lc_format) {
lc_format = DEFAULT_DATE_TIME_FORMAT;
}
DateFormat formatter = new SimpleDateFormat(lc_format);
String dateStr = formatter.format(new Date(value));
if (DEFAULT_DATE_TIME_FORMAT == lc_format) {
// hack to be 100% W3
dateStr = dateStr.substring(0, dateStr.length()-2) + ':' + dateStr.substring(dateStr.length()-2);
}
return dateStr;
}
/*
* Can't use new Date(value) with formatter... :-(
*/
public static String timeIntervalToFormattedString(int in_milliseconds) {
StringBuffer buff = new StringBuffer(9);
long rest = in_milliseconds / 1000; // seconds
long hour = rest / 3600;
rest = rest % 3600;
long minute = rest / 60;
rest = rest % 60;
long second = rest;
if (hour < 10) {
buff.append('0');
}
buff.append(hour);
buff.append(':');
if (minute < 10) {
buff.append('0');
}
buff.append(minute);
buff.append(':');
if (second < 10) {
buff.append('0');
}
buff.append(second);
return buff.toString();
}
public static String timeIntervalToFormattedString(long in_milliseconds) {
StringBuffer buff = new StringBuffer(9);
int millis = (int) in_milliseconds % 1000;
long rest = in_milliseconds / 1000; // seconds
long hour = rest / 3600;
rest = rest % 3600;
long minute = rest / 60;
rest = rest % 60;
long second = rest;
if (hour >= 24) {
buff.append(I18NSupport.getLocalizedMessage("days", new Long[] {new Long(hour/24)}));//$NON-NLS-1$
hour = hour % 24;
}
if (hour < 10) {
buff.append('0');
}
buff.append(hour);
buff.append(':');
if (minute < 10) {
buff.append('0');
}
buff.append(minute);
buff.append(':');
if (second < 10) {
buff.append('0');
}
buff.append(second);
return buff.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy