Skip to main content
summaryrefslogtreecommitdiffstats
blob: 914eea3abb1dd6413193d303686915ff6a631f47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.commons.core;

import java.util.Calendar;

import org.eclipse.core.runtime.Assert;
import org.eclipse.osgi.util.NLS;

/**
 * Used for formatting dates.
 * 
 * @author Mik Kersten
 * @since 3.0
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class DateUtil {

	public static String getIsoFormattedDate(Calendar calendar) {
		try {
			int monthInt = (calendar.get(Calendar.MONTH) + 1);
			String month = "" + monthInt; //$NON-NLS-1$
			if (monthInt < 10) {
				month = "0" + month; //$NON-NLS-1$
			}
			int dateInt = (calendar.get(Calendar.DATE));
			String date = "" + dateInt; //$NON-NLS-1$
			if (dateInt < 10) {
				date = "0" + date; //$NON-NLS-1$
			}
			return calendar.get(Calendar.YEAR) + "-" + month + "-" + date; //$NON-NLS-1$ //$NON-NLS-2$
		} catch (Exception e) {
			return "<unresolved date>"; //$NON-NLS-1$
		}
	}

	/**
	 * @return Time formatted according to: http://www.iso.org/iso/date_and_time_format
	 */
	public static String getIsoFormattedDateTime(Calendar calendar) {
		return getIsoFormattedDate(calendar) + "T" + calendar.get(Calendar.HOUR) + "-" + calendar.get(Calendar.MINUTE) //$NON-NLS-1$ //$NON-NLS-2$
				+ "-" + calendar.get(Calendar.SECOND); //$NON-NLS-1$
	}

	/**
	 * Returns the time in the format: HHH:MM. If <code>includeSeconds</code> is true, the returned format is:
	 * HHH:MM:SS.
	 * 
	 * @since 3.5
	 */
	public static String getFormattedDurationShort(long duration, boolean includeSeconds) {
		if (duration <= 0) {
			return "00:00"; //$NON-NLS-1$
		}

		long totalSeconds = duration / 1000;
		long remainderSeconds = totalSeconds % 60;
		long totalMinutes = totalSeconds / 60;
		long remainderMinutes = totalMinutes % 60;
		long totalHours = totalMinutes / 60;

		StringBuffer sb = new StringBuffer(8);
		sb.append(totalHours);
		if (totalHours < 10) {
			sb.append("0"); //$NON-NLS-1$
		}
		sb.append(":"); //$NON-NLS-1$
		sb.append(remainderMinutes);
		if (remainderMinutes < 10) {
			sb.append("0"); //$NON-NLS-1$
		}
		if (includeSeconds) {
			sb.append(":"); //$NON-NLS-1$
			sb.append(remainderSeconds);
			if (remainderSeconds < 10) {
				sb.append("0"); //$NON-NLS-1$
			}
		}
		return sb.toString();
	}

	/** Returns the time in the format: HHH:MM */
	public static String getFormattedDurationShort(long duration) {
		return getFormattedDurationShort(duration, false);
	}

	/**
	 * @deprecated The result of this method is not properly localized.
	 */
	@Deprecated
	public static String getFormattedDuration(long duration, boolean includeSeconds) {
		long seconds = duration / 1000;
		long minutes = 0;
		long hours = 0;
		// final long SECOND = 1000;
		final long MIN = 60;
		final long HOUR = MIN * 60;
		String formatted = ""; //$NON-NLS-1$

		String hour = ""; //$NON-NLS-1$
		String min = ""; //$NON-NLS-1$
		String sec = ""; //$NON-NLS-1$
		if (seconds >= HOUR) {
			hours = seconds / HOUR;
			if (hours == 1) {
				hour = hours + " hour "; //$NON-NLS-1$
			} else if (hours > 1) {
				hour = hours + " hours "; //$NON-NLS-1$
			}
			seconds -= hours * HOUR;

			minutes = seconds / MIN;
			if (minutes == 1) {
				min = minutes + " minute "; //$NON-NLS-1$
			} else if (minutes != 1) {
				min = minutes + " minutes "; //$NON-NLS-1$
			}
			seconds -= minutes * MIN;
			if (seconds == 1) {
				sec = seconds + " second"; //$NON-NLS-1$
			} else if (seconds > 1) {
				sec = seconds + " seconds"; //$NON-NLS-1$
			}
			formatted += hour + min;
			if (includeSeconds) {
				formatted += sec;
			}
		} else if (seconds >= MIN) {
			minutes = seconds / MIN;
			if (minutes == 1) {
				min = minutes + " minute "; //$NON-NLS-1$
			} else if (minutes != 1) {
				min = minutes + " minutes "; //$NON-NLS-1$
			}
			seconds -= minutes * MIN;
			if (seconds == 1) {
				sec = seconds + " second"; //$NON-NLS-1$
			} else if (seconds > 1) {
				sec = seconds + " seconds"; //$NON-NLS-1$
			}
			formatted += min;
			if (includeSeconds) {
				formatted += sec;
			}
		} else {
			if (seconds == 1) {
				sec = seconds + " second"; //$NON-NLS-1$
			} else if (seconds > 1) {
				sec = seconds + " seconds"; //$NON-NLS-1$
			}
			if (includeSeconds) {
				formatted += sec;
			}
		}
		return formatted;
	}

	private static long MILLIS_SECOND = 1000;

	private static long MILLIS_MINUTE = 60 * MILLIS_SECOND;

	private static long MILLIS_HOUR = 60 * MILLIS_MINUTE;

	private static long MILLIS_DAY = 24 * MILLIS_HOUR;

	private static long MILLIS_WEEK = 7 * MILLIS_DAY;

	private static long MILLIS_MONTH = 4 * MILLIS_WEEK;

	private enum Period {

		MONTH(MILLIS_MONTH, "{0} month", "{0} month"), //
		WEEK(MILLIS_WEEK, "{0} week", "{0} weeks"), // 
		DAY(MILLIS_DAY, "{0} day", "{0} days"), //
		HOUR(MILLIS_HOUR, "{0} hr", "{0} hrs"), //
		MINUTE(MILLIS_MINUTE, "{0} min", "{0} mins"), //
		SECOND(MILLIS_SECOND, "{0} sec", "{0} secs");

		private final long duration;

		private final String singularLabel;

		private final String pluralLabel;

		Period(long duration, String singularLabel, String pluralLabel) {
			this.duration = duration;
			this.singularLabel = singularLabel;
			this.pluralLabel = pluralLabel;
		}

		public String toString(long time) {
			long count = time / duration;
			if (count <= 1) {
				return NLS.bind(singularLabel, count);
			} else {
				return NLS.bind(pluralLabel, count);
			}
		}

	}

	private static class PeriodString {

		private final Period period;

		private final long duration;

		private PeriodString next;

		public PeriodString(Period period, long duration) {
			Assert.isNotNull(period);
			this.period = period;
			this.duration = duration;
		}

		public void append(PeriodString next) {
			this.next = next;
		}

		@Override
		public String toString() {
			return period.toString(duration) + ((next != null) ? " " + next.toString() : ""); //$NON-NLS-1$ //$NON-NLS-2$
		}

	}

	/**
	 * @since 3.5
	 */
	public static String getRelative(long time) {
		long diff = System.currentTimeMillis() - time;
		String duration = getRelativeDuration(Math.abs(diff));
		if (duration.length() > 0) {
			if (diff > 0) {
				return NLS.bind("{0} ago", duration);
			} else {
				return NLS.bind("in {0}", duration);
			}
		}
		return "";
	}

	/**
	 * @since 3.5
	 */
	public static String getRelativeDuration(long diff) {
		PeriodString string = null;
		for (Period period : Period.values()) {
			boolean wasSet = (string != null);
			if (diff >= period.duration) {
				if (string == null) {
					string = new PeriodString(period, diff);
				} else {
					string.append(new PeriodString(period, diff));
					// do not add more than two segments
					break;
				}
				diff -= (diff / period.duration) * period.duration;
			}
			// only return more than one segment if the second segment follows the first one directly 
			if (wasSet) {
				break;
			}
		}
		return (string != null) ? string.toString() : ""; //$NON-NLS-1$
	}

}

Back to the top