use of java.text.DateFormat in project Openfire by igniterealtime.
the class JiveGlobals method formatDate.
/**
* Formats a Date object to return a date using the global locale.
*
* @param date the Date to format.
* @return a String representing the date.
*/
public static String formatDate(Date date) {
if (dateFormat == null) {
if (properties != null) {
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
dateFormat.setTimeZone(getTimeZone());
} else {
DateFormat instance = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale());
instance.setTimeZone(getTimeZone());
return instance.format(date);
}
}
return dateFormat.format(date);
}
use of java.text.DateFormat in project Openfire by igniterealtime.
the class ResourceServlet method service.
@Override
public void service(HttpServletRequest request, HttpServletResponse response) {
boolean compress = false;
boolean javascript = request.getRequestURI().endsWith("scripts/");
if (!disableCompression) {
if (request.getHeader("accept-encoding") != null && request.getHeader("accept-encoding").contains("gzip")) {
compress = true;
} else if (request.getHeader("---------------") != null) {
// norton internet security
compress = true;
}
}
if (javascript) {
response.setHeader("Content-type", "text/javascript");
} else {
response.setHeader("Content-type", "text/css");
}
// Handle proxies
response.setHeader("Vary", "Accept-Encoding");
if (!debug) {
DateFormat formatter = new SimpleDateFormat("d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
response.setHeader("Expires", formatter.format(new Date(System.currentTimeMillis() + expiresOffset)));
response.setHeader("Cache-Control", "max-age=" + expiresOffset);
} else {
response.setHeader("Expires", "1");
compress = false;
}
try {
byte[] content;
String cacheKey = String.valueOf(compress + " " + javascript);
content = cache.get(cacheKey);
if (javascript && (debug || content == null)) {
content = getJavaScriptContent(compress);
cache.put(cacheKey, content);
} else if (!javascript && content == null) {
}
response.setContentLength(content.length);
if (compress) {
response.setHeader("Content-Encoding", "gzip");
}
// Write the content out
try (ByteArrayInputStream in = new ByteArrayInputStream(content)) {
try (OutputStream out = response.getOutputStream()) {
// Use a 128K buffer.
byte[] buf = new byte[128 * 1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
out.flush();
}
}
} catch (IOException e) {
Log.error(e.getMessage(), e);
}
}
use of java.text.DateFormat in project Openfire by igniterealtime.
the class GraphEngine method createTickUnits.
private TickUnits createTickUnits(Locale locale, TimeZone zone) {
TickUnits units = new TickUnits();
// date formatters
DateFormat f1 = new SimpleDateFormat("HH:mm:ss.SSS", locale);
DateFormat f2 = new SimpleDateFormat("HH:mm:ss", locale);
DateFormat f3 = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
DateFormat f4 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
DateFormat f5 = new SimpleDateFormat("d-MMM", locale);
DateFormat f6 = new SimpleDateFormat("MMM-yyyy", locale);
DateFormat f7 = new SimpleDateFormat("yyyy", locale);
// NOTE: timezone not needed on date formatters because dates have already been converted
// to the appropriate timezone by the respective RegularTimePeriod (Minute, Hour, Day, etc)
// see:
// http://www.jfree.org/jfreechart/api/gjdoc/org/jfree/data/time/Hour.html#Hour:Date:TimeZone
//
// If you do use a timezone on the formatters and the Jive TimeZone has been set to something
// other than the system timezone, time specific charts will show incorrect values.
/*
f1.setTimeZone(zone);
f2.setTimeZone(zone);
f3.setTimeZone(zone);
f4.setTimeZone(zone);
f5.setTimeZone(zone);
f6.setTimeZone(zone);
f7.setTimeZone(zone);
*/
// milliseconds
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 5, DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 10, DateTickUnit.MILLISECOND, 1, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 25, DateTickUnit.MILLISECOND, 5, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 50, DateTickUnit.MILLISECOND, 10, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 100, DateTickUnit.MILLISECOND, 10, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 250, DateTickUnit.MILLISECOND, 10, f1));
units.add(new DateTickUnit(DateTickUnit.MILLISECOND, 500, DateTickUnit.MILLISECOND, 50, f1));
// seconds
units.add(new DateTickUnit(DateTickUnit.SECOND, 1, DateTickUnit.MILLISECOND, 50, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 5, DateTickUnit.SECOND, 1, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 10, DateTickUnit.SECOND, 1, f2));
units.add(new DateTickUnit(DateTickUnit.SECOND, 30, DateTickUnit.SECOND, 5, f2));
// minutes
units.add(new DateTickUnit(DateTickUnit.MINUTE, 1, DateTickUnit.SECOND, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 2, DateTickUnit.SECOND, 10, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 5, DateTickUnit.MINUTE, 1, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 10, DateTickUnit.MINUTE, 1, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 15, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 20, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.MINUTE, 30, DateTickUnit.MINUTE, 5, f3));
// hours
units.add(new DateTickUnit(DateTickUnit.HOUR, 1, DateTickUnit.MINUTE, 5, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 2, DateTickUnit.MINUTE, 10, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 4, DateTickUnit.MINUTE, 30, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 6, DateTickUnit.HOUR, 1, f3));
units.add(new DateTickUnit(DateTickUnit.HOUR, 12, DateTickUnit.HOUR, 1, f4));
// days
units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, f5));
units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, f5));
// months
units.add(new DateTickUnit(DateTickUnit.MONTH, 1, DateTickUnit.DAY, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 2, DateTickUnit.DAY, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 3, DateTickUnit.MONTH, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 4, DateTickUnit.MONTH, 1, f6));
units.add(new DateTickUnit(DateTickUnit.MONTH, 6, DateTickUnit.MONTH, 1, f6));
// years
units.add(new DateTickUnit(DateTickUnit.YEAR, 1, DateTickUnit.MONTH, 1, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 2, DateTickUnit.MONTH, 3, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 5, DateTickUnit.YEAR, 1, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 10, DateTickUnit.YEAR, 1, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 25, DateTickUnit.YEAR, 5, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 50, DateTickUnit.YEAR, 10, f7));
units.add(new DateTickUnit(DateTickUnit.YEAR, 100, DateTickUnit.YEAR, 20, f7));
return units;
}
use of java.text.DateFormat in project Openfire by igniterealtime.
the class GraphEngine method parseTimePeriod.
public static long[] parseTimePeriod(String timeperiod) {
if (null == timeperiod)
timeperiod = "last60minutes";
Date fromDate = null;
Date toDate = null;
long dataPoints = 60;
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
// Reset the day fields so we're at the beginning of the day.
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
// Compute "this week" by resetting the day of the week to the first day of the week
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
Date thisWeekStart = cal.getTime();
Date thisWeekEnd = now;
// Compute last week - start with the end boundary which is 1 millisecond before the start of this week
cal.add(Calendar.MILLISECOND, -1);
Date lastWeekEnd = cal.getTime();
// Add that millisecond back, subtract 7 days for the start boundary of "last week"
cal.add(Calendar.MILLISECOND, 1);
cal.add(Calendar.DAY_OF_YEAR, -7);
Date lastWeekStart = cal.getTime();
// Reset the time
cal.setTime(now);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
// Reset to the 1st day of the month, make the the start boundary for "this month"
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
Date thisMonthStart = cal.getTime();
Date thisMonthEnd = now;
// Compute last month
cal.add(Calendar.MILLISECOND, -1);
Date lastMonthEnd = cal.getTime();
cal.add(Calendar.MILLISECOND, 1);
cal.add(Calendar.MONTH, -1);
Date lastMonthStart = cal.getTime();
// Compute last 3 months
cal.setTime(now);
cal.add(Calendar.MONTH, -2);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date last3MonthsStart = cal.getTime();
Date last3MonthsEnd = now;
// Compute last 7 days:
cal.setTime(now);
cal.add(Calendar.DAY_OF_YEAR, -6);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date last7DaysStart = cal.getTime();
Date last7DaysEnd = now;
// Compute last 60 minutes;
cal.setTime(now);
cal.add(Calendar.MINUTE, -60);
Date last60MinutesStart = cal.getTime();
Date last60MinutesEnd = now;
// Compute last 24 hours;
cal.setTime(now);
cal.add(Calendar.HOUR, -23);
Date last24HoursStart = cal.getTime();
Date last24HoursEnd = now;
// Done, reset the cal internal date to now
cal.setTime(now);
if ("thisweek".equals(timeperiod)) {
fromDate = thisWeekStart;
toDate = thisWeekEnd;
dataPoints = 7;
} else if ("last7days".equals(timeperiod)) {
fromDate = last7DaysStart;
toDate = last7DaysEnd;
dataPoints = 7;
} else if ("lastweek".equals(timeperiod)) {
fromDate = lastWeekStart;
toDate = lastWeekEnd;
dataPoints = 7;
} else if ("thismonth".equals(timeperiod)) {
fromDate = thisMonthStart;
toDate = thisMonthEnd;
dataPoints = 30;
} else if ("lastmonth".equals(timeperiod)) {
fromDate = lastMonthStart;
toDate = lastMonthEnd;
dataPoints = 30;
} else if ("last3months".equals(timeperiod)) {
fromDate = last3MonthsStart;
toDate = last3MonthsEnd;
dataPoints = (long) Math.ceil((toDate.getTime() - fromDate.getTime()) / WEEK);
} else if ("last60minutes".equals(timeperiod)) {
fromDate = last60MinutesStart;
toDate = last60MinutesEnd;
dataPoints = 60;
} else if ("last24hours".equals(timeperiod)) {
fromDate = last24HoursStart;
toDate = last24HoursEnd;
dataPoints = 48;
} else {
String[] dates = timeperiod.split("to");
if (dates.length > 0) {
DateFormat formDateFormatter = new SimpleDateFormat("MM/dd/yy");
String fromDateParam = dates[0];
String toDateParam = dates[1];
if (fromDateParam != null) {
try {
fromDate = formDateFormatter.parse(fromDateParam);
} catch (Exception e) {
// ignore formatting exception
}
}
if (toDateParam != null) {
try {
toDate = formDateFormatter.parse(toDateParam);
// Make this date be the end of the day (so it's the day *inclusive*, not *exclusive*)
Calendar adjusted = Calendar.getInstance();
adjusted.setTime(toDate);
adjusted.set(Calendar.HOUR_OF_DAY, 23);
adjusted.set(Calendar.MINUTE, 59);
adjusted.set(Calendar.SECOND, 59);
adjusted.set(Calendar.MILLISECOND, 999);
toDate = adjusted.getTime();
} catch (Exception e) {
// ignore formatting exception
}
}
dataPoints = discoverDataPoints(fromDate, toDate);
}
}
// default to last 60 minutes
if (null == fromDate && null == toDate) {
return new long[] { last60MinutesStart.getTime(), last60MinutesEnd.getTime(), dataPoints };
} else if (null == fromDate) {
return new long[] { 0, toDate.getTime(), dataPoints };
} else if (null == toDate) {
return new long[] { fromDate.getTime(), now.getTime(), dataPoints };
} else {
return new long[] { fromDate.getTime(), toDate.getTime(), dataPoints };
}
}
use of java.text.DateFormat in project FastDev4Android by jiangqqlmj.
the class StrUtils method getSystemTime.
// 获取系统时间
public static String getSystemTime() {
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = format.format(date);
return result;
}
Aggregations