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

clime.messadmin.utils.DateUtils Maven / Gradle / Ivy

Go to download

Notification system and Session administration for J2EE Web Applications

There is a newer version: 4.1.1
Show newest version
/**
 * 
 */
package clime.messadmin.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Cédrik LIME
 */
public abstract class DateUtils {
	/** @see ISO 8601 DateTime */
	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% ISO
			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(hour / 24).append(" day");
			if (hour/24 > 1) {
				buff.append('s');
			}
			buff.append(", ");
			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 - 2024 Weber Informatics LLC | Privacy Policy